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 "external_wifi_common_builder_manager.h"
17 #include "wifi_logger.h"
18 #include "wifi_scorer_impl.h"
19
20
21 namespace OHOS::Wifi {
22 DEFINE_WIFILOG_LABEL("WifiCommonBuilderManager")
23
GetInstance()24 ExternalWifiCommonBuildManager &ExternalWifiCommonBuildManager::GetInstance()
25 {
26 static ExternalWifiCommonBuildManager gNetworkSelectorCommonBuilderManager;
27 return gNetworkSelectorCommonBuilderManager;
28 }
29
RegisterCommonBuilder(const TagType & tagType,const std::string & tagName,const CommonBuilder & commonBuilder)30 void ExternalWifiCommonBuildManager::RegisterCommonBuilder(const TagType &tagType, const std::string &tagName,
31 const CommonBuilder &commonBuilder)
32 {
33 if (commonBuilder.IsEmpty()) {
34 WIFI_LOGE("the scoreBuilder for tagType: %{public}d, tagName: %{public}s is empty",
35 static_cast<int>(tagType), tagName.c_str());
36 return;
37 }
38 std::lock_guard<std::mutex> lock(mutex);
39 WIFI_LOGI("RegisterCommonBuilder for tagType: %{public}d, tagName: %{public}s",
40 static_cast<int>(tagType), tagName.c_str());
41 if (commonBuilders.size() > REGISTERINFO_MAX_NUM) {
42 WIFI_LOGW("%{public}s fail commonBuilders size is: %{public}d, over 1000",
43 __FUNCTION__, static_cast<int>(commonBuilders.size()));
44 return;
45 }
46 commonBuilders.insert_or_assign({tagType, tagName}, commonBuilder);
47 }
48
DeregisterCommonBuilder(const TagType & tagType,const std::string & tagName)49 void ExternalWifiCommonBuildManager::DeregisterCommonBuilder(const TagType &tagType, const std::string &tagName)
50 {
51 std::lock_guard<std::mutex> lock(mutex);
52 WIFI_LOGI("DeregisterCommonBuilder for tagType: %{public}d, tagName: %{public}s",
53 static_cast<int>(tagType), tagName.c_str());
54 commonBuilders.erase({tagType, tagName});
55 }
56
BuildScore(const TagType & scoreTag,NetworkSelection::CompositeWifiScorer & compositeScore)57 void ExternalWifiCommonBuildManager::BuildScore(const TagType &scoreTag,
58 NetworkSelection::CompositeWifiScorer &compositeScore)
59 {
60 std::lock_guard<std::mutex> lock(mutex);
61 for (const auto &commonBuilderPair : commonBuilders) {
62 /* find the builder which match the filterTag */
63 if (commonBuilderPair.first.first != scoreTag) {
64 continue;
65 }
66 commonBuilderPair.second.scoreBuilder.operator()(compositeScore);
67 }
68 }
69
BuildFilter(const TagType & filterTag,NetworkSelection::CompositeWifiFilter & compositeFilter)70 void ExternalWifiCommonBuildManager::BuildFilter(const TagType &filterTag,
71 NetworkSelection::CompositeWifiFilter &compositeFilter)
72 {
73 std::lock_guard<std::mutex> lock(mutex);
74 for (const auto &commonBuilderPair : commonBuilders) {
75 /* find the builder which match the filterTag */
76 if (commonBuilderPair.first.first != filterTag) {
77 continue;
78 }
79 commonBuilderPair.second.filterBuilder.operator()(compositeFilter);
80 }
81 }
82 }
83