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';
17
18/**
19 * Obtains traffic statistics.
20 *
21 * @since 10
22 * @syscap SystemCapability.Communication.NetManager.Core
23 */
24declare namespace statistics {
25  /**
26   * Queries the data traffic (including all TCP and UDP data packets) received through a specified NIC.
27   *
28   * @param nic Indicates the NIC name.
29   * @param callback Returns the data traffic received through the specified NIC.
30   */
31  function getIfaceRxBytes(nic: string, callback: AsyncCallback<number>): void;
32  function getIfaceRxBytes(nic: string): Promise<number>;
33
34  /**
35   * Queries the data traffic (including all TCP and UDP data packets) sent through a specified NIC.
36   *
37   * @param nic Indicates the NIC name.
38   * @param callback Returns the data traffic sent through the specified NIC.
39   */
40  function getIfaceTxBytes(nic: string, callback: AsyncCallback<number>): void;
41  function getIfaceTxBytes(nic: string): Promise<number>;
42
43  /**
44   * Queries the data traffic (including all TCP and UDP data packets) received through the cellular network.
45   *
46   * @param callback Returns the data traffic received through the cellular network.
47   */
48  function getCellularRxBytes(callback: AsyncCallback<number>): void;
49  function getCellularRxBytes(): Promise<number>;
50
51  /**
52   * Queries the data traffic (including all TCP and UDP data packets) sent through the cellular network.
53   *
54   * @param callback Returns the data traffic sent through the cellular network.
55   */
56  function getCellularTxBytes(callback: AsyncCallback<number>): void;
57  function getCellularTxBytes(): Promise<number>;
58
59  /**
60   * Queries the data traffic (including all TCP and UDP data packets) sent through all NICs.
61   *
62   * @param callback Returns the data traffic sent through all NICs.
63   */
64  function getAllTxBytes(callback: AsyncCallback<number>): void;
65  function getAllTxBytes(): Promise<number>;
66
67  /**
68   * Queries the data traffic (including all TCP and UDP data packets) received through all NICs.
69   *
70   * @param callback Returns the data traffic received through all NICs.
71   */
72  function getAllRxBytes(callback: AsyncCallback<number>): void;
73  function getAllRxBytes(): Promise<number>;
74
75  /**
76   * Queries the data traffic (including all TCP and UDP data packets) received by a specified application.
77   * This method applies only to system applications and your own applications.
78   *
79   * @param uid Indicates the process ID of the application.
80   * @param callback Returns the data traffic received by the specified application.
81   */
82  function getUidRxBytes(uid: number, callback: AsyncCallback<number>): void;
83  function getUidRxBytes(uid: number): Promise<number>;
84
85  /**
86   * Queries the data traffic (including all TCP and UDP data packets) sent by a specified application.
87   * This method applies only to system applications and your own applications.
88   *
89   * @param uid Indicates the process ID of the application.
90   * @param callback Returns the data traffic sent by the specified application.
91   */
92  function getUidTxBytes(uid: number, callback: AsyncCallback<number>): void;
93  function getUidTxBytes(uid: number): Promise<number>;
94
95  /**
96   * Register notifications of network traffic updates.
97   *
98   * @permission ohos.permission.CONNECTIVITY_INTERNAL
99   * @systemapi Hide this for inner system use.
100   */
101  function on(type: 'netStatsChange', callback: Callback<{ iface: string, uid?: number }>): void;
102
103  /**
104   * @systemapi Hide this for inner system use.
105   */
106  function off(type: 'netStatsChange', callback?: Callback<{ iface: string, uid?: number }>): void;
107
108  /**
109   * Get the traffic usage details of the network interface in the specified time period.
110   *
111   * @param ifaceInfo Indicates the handle. See {@link IfaceInfo}.
112   * @permission ohos.permission.CONNECTIVITY_INTERNAL
113   * @systemapi Hide this for inner system use.
114   */
115  function getIfaceStats(ifaceInfo: IfaceInfo, callback: AsyncCallback<NetStatsInfo>): void;
116  function getIfaceStats(ifaceInfo: IfaceInfo): Promise<NetStatsInfo>;
117
118  /**
119   * Get the traffic usage details of the specified time period of the application.
120   *
121   * @param uidStatsInfo Indicates the handle. See {@link UidStatsInfo}.
122   * @permission ohos.permission.CONNECTIVITY_INTERNAL
123   * @systemapi Hide this for inner system use.
124   */
125  function getIfaceUidStats(uidStatsInfo: UidStatsInfo, callback: AsyncCallback<NetStatsInfo>): void;
126  function getIfaceUidStats(uidStatsInfo: UidStatsInfo): Promise<NetStatsInfo>;
127
128  /**
129   * @systemapi Hide this for inner system use.
130   */
131  export interface IfaceInfo {
132    iface: string;
133    startTime: number;
134    endTime: number;
135  }
136
137  /**
138   * @systemapi Hide this for inner system use.
139   */
140  export interface UidStatsInfo {
141    /*See {@link IfaceInfo}*/
142    ifaceInfo: IfaceInfo;
143    uid: number;
144  }
145
146  /**
147   * @systemapi Hide this for inner system use.
148   */
149  export interface NetStatsInfo {
150    rxBytes: number;
151    txBytes: number;
152    rxPackets: number;
153    txPackets: number;
154  }
155}
156
157export default statistics;