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