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