1 /*
2 * Copyright (C) 2021 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 "traffic_management.h"
17
18 #include <cinttypes>
19 #include "core_manager_inner.h"
20 #include "data_flow_statistics.h"
21 #include "net_conn_client.h"
22 #include "telephony_log_wrapper.h"
23
24 namespace OHOS {
25 namespace Telephony {
26 using namespace NetManagerStandard;
27
TrafficManagement(int32_t slotId)28 TrafficManagement::TrafficManagement(int32_t slotId) : slotId_(slotId) {}
29
30 TrafficManagement::~TrafficManagement() = default;
31
GetPacketData(int64_t & sendPackets,int64_t & recvPackets)32 void TrafficManagement::GetPacketData(int64_t &sendPackets, int64_t &recvPackets)
33 {
34 sendPackets = sendPackets_;
35 recvPackets = recvPackets_;
36 }
37
UpdatePacketData()38 void TrafficManagement::UpdatePacketData()
39 {
40 DataFlowStatistics dataState;
41 const std::string interfaceName = GetIfaceName();
42 if (!interfaceName.empty()) {
43 sendPackets_ = dataState.GetIfaceTxPackets(interfaceName);
44 recvPackets_ = dataState.GetIfaceRxPackets(interfaceName);
45 }
46 TELEPHONY_LOGD("Slot%{public}d: sendPackets:%{public}" PRId64 " recvPackets:%{public}" PRId64,
47 slotId_, sendPackets_, recvPackets_);
48 }
49
GetIfaceName()50 std::string TrafficManagement::GetIfaceName()
51 {
52 std::string ifaceName = "";
53 int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
54 std::list<int32_t> netIdList;
55 int32_t ret = NetConnClient::GetInstance().GetNetIdByIdentifier(IDENT_PREFIX + std::to_string(simId), netIdList);
56 if (ret != NETMANAGER_SUCCESS) {
57 TELEPHONY_LOGE("Slot%{public}d: get netIdList by identifier failed, ret = %{public}d", slotId_, ret);
58 return ifaceName;
59 }
60 std::list<sptr<NetManagerStandard::NetHandle>> netList;
61 int32_t result = NetConnClient::GetInstance().GetAllNets(netList);
62 if (result != NETMANAGER_SUCCESS) {
63 TELEPHONY_LOGE("Slot%{public}d: get all nets failed, ret = %{public}d", slotId_, result);
64 return ifaceName;
65 }
66 for (sptr<NetManagerStandard::NetHandle> netHandle : netList) {
67 for (auto netId : netIdList) {
68 TELEPHONY_LOGD("Slot%{public}d: netId = %{public}d, netHandle->GetNetId() = %{public}d", slotId_, netId,
69 netHandle->GetNetId());
70 if (netId == netHandle->GetNetId()) {
71 NetLinkInfo info;
72 NetConnClient::GetInstance().GetConnectionProperties(*netHandle, info);
73 ifaceName = info.ifaceName_;
74 TELEPHONY_LOGD("Slot%{public}d: data is connected ifaceName = %{public}s", slotId_, ifaceName.c_str());
75 return ifaceName;
76 }
77 }
78 }
79 return ifaceName;
80 }
81 } // namespace Telephony
82 } // namespace OHOS
83