1 /*
2  * Copyright (C) 2024 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 "ip_qos_monitor.h"
17 #include "wifi_logger.h"
18 #include "wifi_config_center.h"
19 
20 static const int32_t MIN_DELTA_TCP_TX = 3;
21 static const int32_t QOS_TCP_TX_PKTS = 6;
22 static const int32_t QOS_TCP_RX_PKTS = 7;
23 static const int32_t QOS_MSG_FROM = 9;
24 static const int32_t MIN_PACKET_LEN = 7;
25 static const int32_t CMD_START_MONITOR = 10;
26 static const int32_t CMD_QUERY_PKTS = 15;
27 
28 namespace OHOS {
29 namespace Wifi {
30 DEFINE_WIFILOG_LABEL("IpQosMonitor");
31 
GetInstance()32 IpQosMonitor &IpQosMonitor::GetInstance()
33 {
34     static IpQosMonitor gIpQosMonitor;
35     return gIpQosMonitor;
36 }
37 
StartMonitor(int32_t arg)38 void IpQosMonitor::StartMonitor(int32_t arg)
39 {
40     WIFI_LOGD("enter %{public}s", __FUNCTION__);
41     WifiNetLink::GetInstance().SendQoeCmd(CMD_START_MONITOR, arg);
42 }
43 
QueryPackets(int32_t arg)44 void IpQosMonitor::QueryPackets(int32_t arg)
45 {
46     WIFI_LOGD("enter %{public}s", __FUNCTION__);
47     WifiNetLink::GetInstance().SendQoeCmd(CMD_QUERY_PKTS, arg);
48 }
49 
HandleTcpReportMsgComplete(const std::vector<int64_t> & elems,int32_t cmd)50 void IpQosMonitor::HandleTcpReportMsgComplete(const std::vector<int64_t> &elems, int32_t cmd)
51 {
52     WIFI_LOGD("enter %{public}s", __FUNCTION__);
53     ParseTcpReportMsg(elems, cmd);
54 }
55 
ParseTcpReportMsg(const std::vector<int64_t> & elems,int32_t cmd)56 void IpQosMonitor::ParseTcpReportMsg(const std::vector<int64_t> &elems, int32_t cmd)
57 {
58     if (elems.size() == 0) {
59         WIFI_LOGE("TcpReportMsg elems size is 0");
60         return;
61     }
62     if (cmd == CMD_QUERY_PKTS) {
63         HandleTcpPktsResp(elems);
64     }
65 }
66 
HandleTcpPktsResp(const std::vector<int64_t> & elems)67 void IpQosMonitor::HandleTcpPktsResp(const std::vector<int64_t> &elems)
68 {
69     WIFI_LOGD("enter %{public}s", __FUNCTION__);
70     bool internetGood = ParseNetworkInternetGood(elems);
71     if (internetGood) {
72         mInternetFailedCounter = 0;
73         mInternetSelfCureAllowed = true;
74         mHttpDetectedAllowed = true;
75         return;
76     }
77     mInternetFailedCounter++;
78     WIFI_LOGI("%{public}s: mInternetFailedCounter = %{public}d", __FUNCTION__, mInternetFailedCounter);
79     WifiLinkedInfo linkedInfo;
80     WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
81     int32_t currentRssi = linkedInfo.rssi;
82     int32_t signalLevel = WifiSettings::GetInstance().GetSignalLevel(linkedInfo.rssi, linkedInfo.band, mInstId);
83     if ((mInternetFailedCounter >= 1) && (linkedInfo.connState == ConnState::CONNECTED)) {
84         ISelfCureService *pSelfCureService = WifiServiceManager::GetInstance().GetSelfCureServiceInst(mInstId);
85         if (pSelfCureService == nullptr) {
86             WIFI_LOGE("%{public}s: pSelfCureService is null", __FUNCTION__);
87             return;
88         }
89         if (mHttpDetectedAllowed && signalLevel >= SIGNAL_LEVEL_2) {
90             WIFI_LOGI("%{public}s: start http detect", __FUNCTION__);
91             if (mNetWorkDetect == nullptr) {
92                 mNetWorkDetect = sptr<NetStateObserver>(new NetStateObserver());
93             }
94             mNetWorkDetect->StartWifiDetection();
95             mHttpDetectedAllowed = false;
96             return;
97         }
98         if (AllowSelfCureNetwork(currentRssi)) {
99             pSelfCureService->NotifyInternetFailureDetected(0);
100             mInternetSelfCureAllowed = false;
101         }
102     }
103 }
104 
AllowSelfCureNetwork(int32_t currentRssi)105 bool IpQosMonitor::AllowSelfCureNetwork(int32_t currentRssi)
106 {
107     ISelfCureService *pSelfCureService = WifiServiceManager::GetInstance().GetSelfCureServiceInst(mInstId);
108     if (pSelfCureService == nullptr) {
109         WIFI_LOGE("%{public}s: pSelfCureService is null.", __FUNCTION__);
110         return false;
111     }
112     if (mInternetSelfCureAllowed && currentRssi >= MIN_VAL_LEVEL_3_5 &&
113        (!pSelfCureService->IsSelfCureOnGoing())) {
114         return true;
115     }
116     return false;
117 }
118 
ParseNetworkInternetGood(const std::vector<int64_t> & elems)119 bool IpQosMonitor::ParseNetworkInternetGood(const std::vector<int64_t> &elems)
120 {
121     WIFI_LOGI("enter %{public}s", __FUNCTION__);
122     bool queryResp = (elems[QOS_MSG_FROM] == 0);
123     int32_t packetsLength = static_cast<int32_t>(elems.size());
124     if ((queryResp) && (packetsLength > MIN_PACKET_LEN)) {
125         int64_t tcpTxPkts = elems[QOS_TCP_TX_PKTS];
126         int64_t tcpRxPkts = elems[QOS_TCP_RX_PKTS];
127         WIFI_LOGD("tcpTxPkts = %{public}" PRId64 ", tcpRxPkts = %{public}" PRId64, tcpTxPkts, tcpRxPkts);
128         if ((mLastTcpTxCounter == 0) || (mLastTcpRxCounter == 0)) {
129             mLastTcpTxCounter = tcpTxPkts;
130             mLastTcpRxCounter = tcpRxPkts;
131             WIFI_LOGI("mLastTcpTxCounter = %{public}" PRId64 ", mLastTcpRxCounter = %{public}" PRId64,
132                 mLastTcpTxCounter, mLastTcpRxCounter);
133             return true;
134         }
135         int64_t deltaTcpTxPkts = tcpTxPkts - mLastTcpTxCounter;
136         int64_t deltaTcpRxPkts = tcpRxPkts - mLastTcpRxCounter;
137         WIFI_LOGI("deltaTcpTxPkts = %{public}" PRId64 ", deltaTcpRxPkts = %{public}" PRId64,
138             deltaTcpTxPkts, deltaTcpRxPkts);
139         mLastTcpTxCounter = tcpTxPkts;
140         mLastTcpRxCounter = tcpRxPkts;
141         if (deltaTcpRxPkts == 0) {
142             if (deltaTcpTxPkts >= MIN_DELTA_TCP_TX) {
143                 WIFI_LOGI("%{public}s internetGood: false", __FUNCTION__);
144                 return false;
145             }
146         }
147     }
148     return true;
149 }
150 } // namespace Wifi
151 } // namespace OHOS
152