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_stub.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 #include "net_stats_constants.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
NetStatsCallbackStub()23 NetStatsCallbackStub::NetStatsCallbackStub()
24 {
25     memberFuncMap_[static_cast<uint32_t>(StatsCallBackInterfaceCode::NET_STATS_IFACE_CHANGED)] =
26         &NetStatsCallbackStub::OnNetIfaceStatsChanged;
27     memberFuncMap_[static_cast<uint32_t>(StatsCallBackInterfaceCode::NET_STATS_UID_CHANGED)] =
28         &NetStatsCallbackStub::OnNetUidStatsChanged;
29 }
30 
31 NetStatsCallbackStub::~NetStatsCallbackStub() = default;
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t NetStatsCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34                                               MessageOption &option)
35 {
36     std::u16string myDescripters = NetStatsCallbackStub::GetDescriptor();
37     std::u16string remoteDescripters = data.ReadInterfaceToken();
38     if (myDescripters != remoteDescripters) {
39         NETMGR_LOG_E("Descriptor checked failed");
40         return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
41     }
42 
43     auto itFunc = memberFuncMap_.find(code);
44     if (itFunc != memberFuncMap_.end()) {
45         auto requestFunc = itFunc->second;
46         if (requestFunc != nullptr) {
47             return (this->*requestFunc)(data, reply);
48         }
49     }
50 
51     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
52 }
53 
OnNetIfaceStatsChanged(MessageParcel & data,MessageParcel & reply)54 int32_t NetStatsCallbackStub::OnNetIfaceStatsChanged(MessageParcel &data, MessageParcel &reply)
55 {
56     std::string iface;
57     if (!data.ReadString(iface)) {
58         return NETMANAGER_ERR_READ_DATA_FAIL;
59     }
60 
61     int32_t result = NetIfaceStatsChanged(iface);
62     if (!reply.WriteInt32(result)) {
63         NETMGR_LOG_E("Write parcel failed");
64         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
65     }
66 
67     return NETMANAGER_SUCCESS;
68 }
69 
OnNetUidStatsChanged(MessageParcel & data,MessageParcel & reply)70 int32_t NetStatsCallbackStub::OnNetUidStatsChanged(MessageParcel &data, MessageParcel &reply)
71 {
72     std::string iface;
73     if (!data.ReadString(iface)) {
74         return NETMANAGER_ERR_READ_DATA_FAIL;
75     }
76 
77     uint32_t uid = 0;
78     if (!data.ReadUint32(uid)) {
79         NETMGR_LOG_E("ReadUint32 uid failed");
80         return NETMANAGER_ERR_READ_DATA_FAIL;
81     }
82 
83     int32_t result = NetUidStatsChanged(iface, uid);
84     if (!reply.WriteInt32(result)) {
85         NETMGR_LOG_E("Write parcel failed");
86         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
87     }
88 
89     return NETMANAGER_SUCCESS;
90 }
91 } // namespace NetManagerStandard
92 } // namespace OHOS
93