1 /*
2 * Copyright (C) 2025 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 "wifi_chr_utils.h"
17 #include "wifi_log.h"
18 #include "wifi_common_util.h"
19 #include "wifi_internal_msg.h"
20 #include "wifi_hisysevent.h"
21
22 namespace OHOS {
23 namespace Wifi {
GetInstance()24 WifiChrUtils &WifiChrUtils::GetInstance()
25 {
26 static WifiChrUtils gWifiChrUtils;
27 return gWifiChrUtils;
28 }
29
WifiChrUtils()30 WifiChrUtils::WifiChrUtils()
31 {}
32
AddSignalPollInfoArray(WifiSignalPollInfo signalInfo)33 void WifiChrUtils::AddSignalPollInfoArray(WifiSignalPollInfo signalInfo)
34 {
35 std::unique_lock<std::mutex> lock(signalInfoMutex);
36 signalPollInfoItem_ = signalInfo;
37 if (signalPollInfoArray.size() >= SIGNALARR_LENGTH) {
38 signalPollInfoArray.pop_back();
39 signalPollInfoArray.insert(signalPollInfoArray.begin(), signalInfo);
40 } else {
41 signalPollInfoArray.push_back(signalInfo);
42 }
43 }
44
ClearSignalPollInfoArray()45 void WifiChrUtils::ClearSignalPollInfoArray()
46 {
47 std::unique_lock<std::mutex> lock(signalInfoMutex);
48 signalPollInfoArray.clear();
49 }
50
GetSignalPollInfoArray(std::vector<WifiSignalPollInfo> & wifiSignalPollInfos,int length)51 void WifiChrUtils::GetSignalPollInfoArray(std::vector<WifiSignalPollInfo> &wifiSignalPollInfos, int length)
52 {
53 LOGI("Eneter GetSignalPollInfoArray.");
54 std::unique_lock<std::mutex> lock(signalInfoMutex);
55 int arrayLength = static_cast<int>(signalPollInfoArray.size());
56 if (length > arrayLength) {
57 length = arrayLength;
58 }
59 for (int index = 0; index < length; index++) {
60 wifiSignalPollInfos.push_back(signalPollInfoArray[index]);
61 }
62 }
63
IsBeaconLost(const std::string & bssid,const int32_t signalLevel,const int32_t instId)64 bool WifiChrUtils::IsBeaconLost(const std::string &bssid, const int32_t signalLevel, const int32_t instId)
65 {
66 if (signalLevel < 0) return false;
67 WifiSignalPollInfo wifiCheckInfo;
68 {
69 std::unique_lock<std::mutex> lock(signalInfoMutex);
70 wifiCheckInfo = signalPollInfoItem_;
71 }
72 bool beaconLost = OHOS::Wifi::IsBeaconLost(bssid, wifiCheckInfo);
73 if (beaconLost) {
74 LOGW("Beacon Lost, signalLevel: %{public}d", signalLevel);
75 int32_t errorCode = (signalLevel <= SIGNAL_LEVEL_TWO) ?
76 BeaconLostType::SIGNAL_LEVEL_LOW : BeaconLostType::SIGNAL_LEVEL_HIGH;
77 WriteWifiBeaconLostHiSysEvent(errorCode);
78 }
79 bool beaconAbnormal = OHOS::Wifi::IsBeaconAbnormal(bssid, wifiCheckInfo);
80 if (beaconAbnormal) {
81 LOGW("Beacon Abnormal, signalLevel: %{public}d", signalLevel);
82 WriteWifiBeaconLostHiSysEvent(BEACON_ABNORMAL);
83 }
84 return beaconLost;
85 }
86 } // namespace Wifi
87 } // namespace OHOS