• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "net_stats_service_stub.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
NetStatsServiceStub()22 NetStatsServiceStub::NetStatsServiceStub()
23 {
24     memberFuncMap_[CMD_NSM_REGISTER_NET_STATS_CALLBACK] = &NetStatsServiceStub::OnRegisterNetStatsCallback;
25     memberFuncMap_[CMD_NSM_UNREGISTER_NET_STATS_CALLBACK] = &NetStatsServiceStub::OnUnregisterNetStatsCallback;
26     memberFuncMap_[CMD_GET_IFACE_RXBYTES] = &NetStatsServiceStub::OnGetIfaceRxBytes;
27     memberFuncMap_[CMD_GET_IFACE_TXBYTES] = &NetStatsServiceStub::OnGetIfaceTxBytes;
28     memberFuncMap_[CMD_GET_CELLULAR_RXBYTES] = &NetStatsServiceStub::OnGetCellularRxBytes;
29     memberFuncMap_[CMD_GET_CELLULAR_TXBYTES] = &NetStatsServiceStub::OnGetCellularTxBytes;
30     memberFuncMap_[CMD_GET_ALL_RXBYTES] = &NetStatsServiceStub::OnGetAllRxBytes;
31     memberFuncMap_[CMD_GET_ALL_TXBYTES] = &NetStatsServiceStub::OnGetAllTxBytes;
32     memberFuncMap_[CMD_GET_UID_RXBYTES] = &NetStatsServiceStub::OnGetUidRxBytes;
33     memberFuncMap_[CMD_GET_UID_TXBYTES] = &NetStatsServiceStub::OnGetUidTxBytes;
34 }
35 
36 NetStatsServiceStub::~NetStatsServiceStub() = default;
37 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int32_t NetStatsServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
39                                              MessageOption &option)
40 {
41     NETMGR_LOG_D("stub call start, code = [%{public}d]", code);
42 
43     std::u16string myDescriptor = NetStatsServiceStub::GetDescriptor();
44     std::u16string remoteDescriptor = data.ReadInterfaceToken();
45     if (myDescriptor != remoteDescriptor) {
46         NETMGR_LOG_D("descriptor checked fail");
47         return ERR_FLATTEN_OBJECT;
48     }
49 
50     auto itFunc = memberFuncMap_.find(code);
51     if (itFunc != memberFuncMap_.end()) {
52         auto requestFunc = itFunc->second;
53         if (requestFunc != nullptr) {
54             return (this->*requestFunc)(data, reply);
55         }
56     }
57     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58 }
59 
OnRegisterNetStatsCallback(MessageParcel & data,MessageParcel & reply)60 int32_t NetStatsServiceStub::OnRegisterNetStatsCallback(MessageParcel &data, MessageParcel &reply)
61 {
62     int32_t result = ERR_FLATTEN_OBJECT;
63     sptr<IRemoteObject> remote = data.ReadRemoteObject();
64     if (remote == nullptr) {
65         NETMGR_LOG_E("Callback ptr is nullptr.");
66         reply.WriteInt32(result);
67         return result;
68     }
69 
70     sptr<INetStatsCallback> callback = iface_cast<INetStatsCallback>(remote);
71     result = RegisterNetStatsCallback(callback);
72     reply.WriteInt32(result);
73     return result;
74 }
75 
OnUnregisterNetStatsCallback(MessageParcel & data,MessageParcel & reply)76 int32_t NetStatsServiceStub::OnUnregisterNetStatsCallback(MessageParcel &data, MessageParcel &reply)
77 {
78     int32_t result = ERR_FLATTEN_OBJECT;
79     sptr<IRemoteObject> remote = data.ReadRemoteObject();
80     if (remote == nullptr) {
81         NETMGR_LOG_E("callback ptr is nullptr.");
82         reply.WriteInt32(result);
83         return result;
84     }
85     sptr<INetStatsCallback> callback = iface_cast<INetStatsCallback>(remote);
86     result = UnregisterNetStatsCallback(callback);
87     reply.WriteInt32(result);
88     return result;
89 }
90 
OnGetIfaceRxBytes(MessageParcel & data,MessageParcel & reply)91 int32_t NetStatsServiceStub::OnGetIfaceRxBytes(MessageParcel &data, MessageParcel &reply)
92 {
93     std::string iface;
94     if (!data.ReadString(iface)) {
95         return ERR_FLATTEN_OBJECT;
96     }
97     int64_t result = GetIfaceRxBytes(iface);
98     if (!reply.WriteInt64(result)) {
99         return ERR_FLATTEN_OBJECT;
100     }
101     return ERR_NONE;
102 }
103 
OnGetIfaceTxBytes(MessageParcel & data,MessageParcel & reply)104 int32_t NetStatsServiceStub::OnGetIfaceTxBytes(MessageParcel &data, MessageParcel &reply)
105 {
106     std::string iface;
107     if (!data.ReadString(iface)) {
108         return ERR_FLATTEN_OBJECT;
109     }
110     int64_t result = GetIfaceTxBytes(iface);
111     if (!reply.WriteInt64(result)) {
112         return ERR_FLATTEN_OBJECT;
113     }
114     return ERR_NONE;
115 }
116 
OnGetCellularRxBytes(MessageParcel & data,MessageParcel & reply)117 int32_t NetStatsServiceStub::OnGetCellularRxBytes(MessageParcel &data, MessageParcel &reply)
118 {
119     if (!reply.WriteInt64(GetCellularRxBytes())) {
120         NETMGR_LOG_E("WriteInt64 failed");
121         return ERR_FLATTEN_OBJECT;
122     }
123     return ERR_NONE;
124 }
125 
OnGetCellularTxBytes(MessageParcel & data,MessageParcel & reply)126 int32_t NetStatsServiceStub::OnGetCellularTxBytes(MessageParcel &data, MessageParcel &reply)
127 {
128     if (!reply.WriteInt64(GetCellularTxBytes())) {
129         NETMGR_LOG_E("WriteInt64 failed");
130         return ERR_FLATTEN_OBJECT;
131     }
132     return ERR_NONE;
133 }
134 
OnGetAllRxBytes(MessageParcel & data,MessageParcel & reply)135 int32_t NetStatsServiceStub::OnGetAllRxBytes(MessageParcel &data, MessageParcel &reply)
136 {
137     if (!reply.WriteInt64(GetAllRxBytes())) {
138         NETMGR_LOG_E("WriteInt64 failed");
139         return ERR_FLATTEN_OBJECT;
140     }
141     return ERR_NONE;
142 }
143 
OnGetAllTxBytes(MessageParcel & data,MessageParcel & reply)144 int32_t NetStatsServiceStub::OnGetAllTxBytes(MessageParcel &data, MessageParcel &reply)
145 {
146     if (!reply.WriteInt64(GetAllTxBytes())) {
147         NETMGR_LOG_E("WriteInt64 failed");
148         return ERR_FLATTEN_OBJECT;
149     }
150     return ERR_NONE;
151 }
152 
OnGetUidRxBytes(MessageParcel & data,MessageParcel & reply)153 int32_t NetStatsServiceStub::OnGetUidRxBytes(MessageParcel &data, MessageParcel &reply)
154 {
155     uint32_t uid;
156     if (!data.ReadUint32(uid)) {
157         return ERR_FLATTEN_OBJECT;
158     }
159 
160     int64_t result = GetUidRxBytes(uid);
161     if (!reply.WriteInt64(result)) {
162         return ERR_FLATTEN_OBJECT;
163     }
164     return ERR_NONE;
165 }
166 
OnGetUidTxBytes(MessageParcel & data,MessageParcel & reply)167 int32_t NetStatsServiceStub::OnGetUidTxBytes(MessageParcel &data, MessageParcel &reply)
168 {
169     uint32_t uid;
170     if (!data.ReadUint32(uid)) {
171         return ERR_FLATTEN_OBJECT;
172     }
173 
174     int64_t result = GetUidTxBytes(uid);
175     if (!reply.WriteInt64(result)) {
176         return ERR_FLATTEN_OBJECT;
177     }
178     return ERR_NONE;
179 }
180 } // namespace NetManagerStandard
181 } // namespace OHOS
182