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_callback_proxy.h"
17
18 #include "net_mgr_log_wrapper.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
NetStatsCallbackProxy(const sptr<IRemoteObject> & impl)22 NetStatsCallbackProxy::NetStatsCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<INetStatsCallback>(impl)
23 {
24 }
25
26 NetStatsCallbackProxy::~NetStatsCallbackProxy() = default;
27
NetIfaceStatsChanged(const std::string & iface)28 int32_t NetStatsCallbackProxy::NetIfaceStatsChanged(const std::string &iface)
29 {
30 MessageParcel data;
31 if (!WriteInterfaceToken(data)) {
32 NETMGR_LOG_E("WriteInterfaceToken failed");
33 return ERR_FLATTEN_OBJECT;
34 }
35 NETMGR_LOG_D("proxy iface[%{public}s]", iface.c_str());
36 if (!data.WriteString(iface)) {
37 return ERR_NULL_OBJECT;
38 }
39
40 sptr<IRemoteObject> remote = Remote();
41 if (remote == nullptr) {
42 NETMGR_LOG_E("Remote is null");
43 return ERR_NULL_OBJECT;
44 }
45
46 MessageParcel reply;
47 MessageOption option;
48 int32_t ret = remote->SendRequest(NET_STATS_IFACE_CHANGED, data, reply, option);
49 if (ret != ERR_NONE) {
50 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
51 }
52 return ret;
53 }
54
NetUidStatsChanged(const std::string & iface,uint32_t uid)55 int32_t NetStatsCallbackProxy::NetUidStatsChanged(const std::string &iface, uint32_t uid)
56 {
57 MessageParcel data;
58 if (!WriteInterfaceToken(data)) {
59 NETMGR_LOG_E("WriteInterfaceToken failed");
60 return ERR_FLATTEN_OBJECT;
61 }
62 NETMGR_LOG_D("proxy iface[%{public}s], uid[%{public}d]", iface.c_str(), uid);
63 if (!data.WriteString(iface)) {
64 return ERR_NULL_OBJECT;
65 }
66
67 if (!data.WriteUint32(uid)) {
68 return ERR_NULL_OBJECT;
69 }
70
71 sptr<IRemoteObject> remote = Remote();
72 if (remote == nullptr) {
73 NETMGR_LOG_E("Remote is null");
74 return ERR_NULL_OBJECT;
75 }
76
77 MessageParcel reply;
78 MessageOption option;
79 int32_t ret = remote->SendRequest(NET_STATS_UID_CHANGED, data, reply, option);
80 if (ret != ERR_NONE) {
81 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
82 }
83 return ret;
84 }
85
WriteInterfaceToken(MessageParcel & data)86 bool NetStatsCallbackProxy::WriteInterfaceToken(MessageParcel &data)
87 {
88 if (!data.WriteInterfaceToken(NetStatsCallbackProxy::GetDescriptor())) {
89 NETMGR_LOG_E("WriteInterfaceToken failed");
90 return false;
91 }
92 return true;
93 }
94 } // namespace NetManagerStandard
95 } // namespace OHOS
96