• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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";
18
19/**
20 * Provides network sharing related interfaces.
21 *
22 * @since 9
23 * @syscap SystemCapability.Communication.NetManager.NetSharing
24 */
25declare namespace sharing {
26  type NetHandle = connection.NetHandle;
27  /**
28   * Checks whether this device allows for network sharing.
29   *
30   * @param callback Returns {@code true} indicating network sharing is supported; returns {@code false} otherwise.
31   * @permission ohos.permission.CONNECTIVITY_INTERNAL
32   * @systemapi Hide this for inner system use.
33   * @throws {BusinessError} 201 - Permission denied.
34   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
35   * @throws {BusinessError} 2200003 - System internal error.
36   * @throws {BusinessError} 2202011 - Cannot get network sharing configuration.
37  */
38  function isSharingSupported(callback: AsyncCallback<boolean>): void;
39  function isSharingSupported(): Promise<boolean>;
40
41  /**
42   * Return the global network sharing state.
43   *
44   * @param callback Returns {@code true} indicating network sharing is running; returns {@code false} otherwise.
45   * @permission ohos.permission.CONNECTIVITY_INTERNAL
46   * @systemapi Hide this for inner system use.
47   * @throws {BusinessError} 201 - Permission denied.
48   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
49   * @throws {BusinessError} 2200003 - System internal error.
50   * @throws {BusinessError} 2202011 - Cannot get network sharing configuration.
51   */
52  function isSharing(callback: AsyncCallback<boolean>): void;
53  function isSharing(): Promise<boolean>;
54
55  /**
56   * Start network sharing for given type.
57   *
58   * @param type Enumeration of shareable interface types.
59   * @param callback Returns the result.
60   * @permission ohos.permission.CONNECTIVITY_INTERNAL
61   * @systemapi Hide this for inner system use.
62   * @throws {BusinessError} 201 - Permission denied.
63   * @throws {BusinessError} 401 - Parameter error.
64   * @throws {BusinessError} 2200001 - Invalid parameter value.
65   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
66   * @throws {BusinessError} 2200003 - System internal error.
67   * @throws {BusinessError} 2202004 - Try to share an unavailable iface.
68   * @throws {BusinessError} 2202005 - WiFi sharing failed.
69   * @throws {BusinessError} 2202006 - Bluetooth sharing failed.
70   * @throws {BusinessError} 2202009 - Network share enable forwarding error.
71   * @throws {BusinessError} 2202011 - Cannot get network sharing configuration.
72   */
73  function startSharing(type: SharingIfaceType, callback: AsyncCallback<void>): void;
74  function startSharing(type: SharingIfaceType): Promise<void>;
75
76  /**
77   * Stop network sharing for given type.
78   *
79   * @param type Enumeration of shareable interface types.
80   * @param callback Returns the result.
81   * @permission ohos.permission.CONNECTIVITY_INTERNAL
82   * @systemapi Hide this for inner system use.
83   * @throws {BusinessError} 201 - Permission denied.
84   * @throws {BusinessError} 401 - Parameter error.
85   * @throws {BusinessError} 2200001 - Invalid parameter value.
86   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
87   * @throws {BusinessError} 2200003 - System internal error.
88   * @throws {BusinessError} 2202004 - Try to share an unavailable iface.
89   * @throws {BusinessError} 2202005 - WiFi sharing failed.
90   * @throws {BusinessError} 2202006 - Bluetooth sharing failed.
91   * @throws {BusinessError} 2202011 - Cannot get network sharing configuration.
92   */
93  function stopSharing(type: SharingIfaceType, callback: AsyncCallback<void>): void;
94  function stopSharing(type: SharingIfaceType): Promise<void>;
95
96  /**
97   * Obtains the number of downlink data bytes of the sharing network interfaces.
98   *
99   * @param callback Returns the number of downlink data bytes of the sharing network interfaces.
100   * @permission ohos.permission.CONNECTIVITY_INTERNAL
101   * @systemapi Hide this for inner system use.
102   * @throws {BusinessError} 201 - Permission denied.
103   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
104   * @throws {BusinessError} 2200003 - System internal error.
105   */
106  function getStatsRxBytes(callback: AsyncCallback<number>): void;
107  function getStatsRxBytes(): Promise<number>;
108
109  /**
110   * Obtains the number of uplink data bytes of the sharing network interfaces.
111   *
112   * @param callback Returns the number of uplink data bytes of the sharing network interfaces.
113   * @permission ohos.permission.CONNECTIVITY_INTERNAL
114   * @systemapi Hide this for inner system use.
115   * @throws {BusinessError} 201 - Permission denied.
116   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
117   * @throws {BusinessError} 2200003 - System internal error.
118   */
119  function getStatsTxBytes(callback: AsyncCallback<number>): void;
120  function getStatsTxBytes(): Promise<number>;
121
122  /**
123   * Obtains the number of total data bytes of the sharing network interfaces.
124   *
125   * @param callback Returns the number of total data bytes of the sharing network interfaces.
126   * @permission ohos.permission.CONNECTIVITY_INTERNAL
127   * @systemapi Hide this for inner system use.
128   * @throws {BusinessError} 201 - Permission denied.
129   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
130   * @throws {BusinessError} 2200003 - System internal error.
131   */
132  function getStatsTotalBytes(callback: AsyncCallback<number>): void;
133  function getStatsTotalBytes(): Promise<number>;
134
135  /**
136   * Obtains the names of interfaces in each sharing state.
137   *
138   * @param state Is the network sharing state.
139   * @param callback Returns an array of interface names that meet this status.
140   * @permission ohos.permission.CONNECTIVITY_INTERNAL
141   * @systemapi Hide this for inner system use.
142   * @throws {BusinessError} 201 - Permission denied.
143   * @throws {BusinessError} 401 - Parameter error.
144   * @throws {BusinessError} 2200001 - Invalid parameter value.
145   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
146   * @throws {BusinessError} 2200003 - System internal error.
147   */
148  function getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback<Array<string>>): void;
149  function getSharingIfaces(state: SharingIfaceState): Promise<Array<string>>;
150
151  /**
152   * Obtains the network sharing state for given type.
153   *
154   * @param type Is the enumeration of shareable interface types.
155   * @param callback Returns {@code SharingIfaceState}.
156   * @permission ohos.permission.CONNECTIVITY_INTERNAL
157   * @systemapi Hide this for inner system use.
158   * @throws {BusinessError} 201 - Permission denied.
159   * @throws {BusinessError} 401 - Parameter error.
160   * @throws {BusinessError} 2200001 - Invalid parameter value.
161   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
162   * @throws {BusinessError} 2200003 - System internal error.
163   */
164  function getSharingState(type: SharingIfaceType, callback: AsyncCallback<SharingIfaceState>): void;
165  function getSharingState(type: SharingIfaceType): Promise<SharingIfaceState>;
166
167  /**
168   * Get a list regular expression that defines any interface that can support network sharing.
169   *
170   * @param type Is the enumeration of shareable interface types.
171   * @param callback Returns an array of regular expression strings that define which interfaces
172   *        are considered to support network sharing.
173   * @permission ohos.permission.CONNECTIVITY_INTERNAL
174   * @systemapi Hide this for inner system use.
175   * @throws {BusinessError} 201 - Permission denied.
176   * @throws {BusinessError} 401 - Parameter error.
177   * @throws {BusinessError} 2200001 - Invalid parameter value.
178   * @throws {BusinessError} 2200002 - Operation failed. Cannot connect to service.
179   * @throws {BusinessError} 2200003 - System internal error.
180   */
181  function getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback<Array<string>>): void;
182  function getSharableRegexes(type: SharingIfaceType): Promise<Array<string>>;
183
184  /**
185   * Register a callback for the global network sharing state change.
186   *
187   * @permission ohos.permission.CONNECTIVITY_INTERNAL
188   * @systemapi Hide this for inner system use.
189   * @throws {BusinessError} 201 Permission denied.
190   * @throws {BusinessError} 401 Parameter error.
191   */
192  function on(type: 'sharingStateChange', callback: Callback<boolean>): void;
193
194  /**
195   * Unregister a callback for the global network sharing state change.
196   *
197   * @permission ohos.permission.CONNECTIVITY_INTERNAL
198   * @systemapi Hide this for inner system use.
199   * @throws {BusinessError} 201 Permission denied.
200   * @throws {BusinessError} 401 Parameter error.
201   */
202  function off(type: 'sharingStateChange', callback?: Callback<boolean>): void;
203
204  /**
205   * Register a callback for the interface network sharing state change.
206   *
207   * @permission ohos.permission.CONNECTIVITY_INTERNAL
208   * @systemapi Hide this for inner system use.
209   * @throws {BusinessError} 201 Permission denied.
210   * @throws {BusinessError} 401 Parameter error.
211   */
212  function on(type: 'interfaceSharingStateChange', callback: Callback<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void;
213
214  /**
215   * Unregister a callback for the interface network sharing state change.
216   *
217   * @permission ohos.permission.CONNECTIVITY_INTERNAL
218   * @systemapi Hide this for inner system use.
219   * @throws {BusinessError} 201 Permission denied.
220   * @throws {BusinessError} 401 Parameter error.
221   */
222  function off(type: 'interfaceSharingStateChange', callback?: Callback<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void;
223
224  /**
225   * Register a callback for the sharing upstream network change.
226   *
227   * @permission ohos.permission.CONNECTIVITY_INTERNAL
228   * @systemapi Hide this for inner system use.
229   * @throws {BusinessError} 201 Permission denied.
230   * @throws {BusinessError} 401 Parameter error.
231   */
232  function on(type: 'sharingUpstreamChange', callback: Callback<NetHandle>): void;
233
234  /**
235   * Unregister a callback for the sharing upstream network change.
236   *
237   * @permission ohos.permission.CONNECTIVITY_INTERNAL
238   * @systemapi Hide this for inner system use.
239   * @throws {BusinessError} 201 Permission denied.
240   * @throws {BusinessError} 401 Parameter error.
241   */
242  function off(type: 'sharingUpstreamChange', callback?: Callback<NetHandle>): void;
243
244  /**
245   * @systemapi Hide this for inner system use.
246   */
247  export enum SharingIfaceState {
248    /**
249     * Indicates the names of the NICs that are serving as network sharing.
250     */
251    SHARING_NIC_SERVING = 1,
252
253    /**
254     * Indicates the names of the NICs that can serve as network sharing.
255     */
256    SHARING_NIC_CAN_SERVER = 2,
257
258    /**
259     * Indicates the names of the NICs that serving error.
260     */
261    SHARING_NIC_ERROR = 3
262  }
263
264  /**
265   * @systemapi Hide this for inner system use.
266   */
267  export enum SharingIfaceType {
268    /**
269     * Network sharing type for Wi-Fi.
270     */
271    SHARING_WIFI = 0,
272
273    /**
274     * Network sharing type for USB.
275     */
276    SHARING_USB = 1,
277
278    /**
279     * Network sharing type for BLUETOOTH.
280     */
281    SHARING_BLUETOOTH = 2
282  }
283}
284
285export default sharing;