1 /*
2  * Copyright (C) 2021-2022 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_common_service_manager.h"
17 #include <dirent.h>
18 #include <iconv.h>
19 #include "wifi_auth_center.h"
20 #include "wifi_config_center.h"
21 #include "wifi_global_func.h"
22 #include "wifi_logger.h"
23 #ifdef OHOS_ARCH_LITE
24 #include "wifi_internal_event_dispatcher_lite.h"
25 #else
26 #include "wifi_internal_event_dispatcher.h"
27 #include "wifi_country_code_manager.h"
28 #include "wifi_app_parser.h"
29 #endif
30 #include "wifi_common_def.h"
31 #include "wifi_common_util.h"
32 #include "wifi_service_manager.h"
33 #ifdef FEATURE_SELF_CURE_SUPPORT
34 #include "ip_qos_monitor.h"
35 #endif
36 
37 namespace OHOS {
38 namespace Wifi {
39 DEFINE_WIFILOG_LABEL("WifiCommonServiceManager");
40 
GetInstance()41 WifiCommonServiceManager &WifiCommonServiceManager::GetInstance()
42 {
43     static WifiCommonServiceManager gWifiCommonServiceManager;
44     return gWifiCommonServiceManager;
45 }
46 
WifiCommonServiceManager()47 WifiCommonServiceManager::WifiCommonServiceManager()
48 {}
49 
~WifiCommonServiceManager()50 WifiCommonServiceManager::~WifiCommonServiceManager()
51 {
52     WifiInternalEventDispatcher::GetInstance().Exit();
53 }
54 
Exit()55 void WifiCommonServiceManager::Exit()
56 {
57     WifiInternalEventDispatcher::GetInstance().Exit();
58 }
Init()59 InitStatus WifiCommonServiceManager::Init()
60 {
61 #ifndef OHOS_ARCH_LITE
62     if (WifiCountryCodeManager::GetInstance().Init() < 0) {
63         WIFI_LOGE("WifiCountryCodeManager Init failed!");
64         return WIFI_COUNTRY_CODE_MANAGER_INIT_FAILED;
65     }
66     using namespace std::placeholders;
67     mWifiAppStateAwareCallbacks.OnForegroundAppChanged =
68         std::bind(&WifiCommonServiceManager::OnForegroundAppChanged, this, _1, _2);
69     if (WifiAppStateAware::GetInstance().InitAppStateAware(mWifiAppStateAwareCallbacks) < 0) {
70         WIFI_LOGE("WifiAppStateAware Init failed!");
71     }
72     if (!AppParser::GetInstance().Init()) {
73         WIFI_LOGE("AppParser Init failed!");
74     }
75     wifiNetAgentCallbacks_.OnRequestNetwork = [this](const int uid, const int networkId) {
76         return this->OnRequestNetwork(uid, networkId);
77     };
78     WifiNetAgent::GetInstance().InitWifiNetAgent(wifiNetAgentCallbacks_);
79 #endif
80 #ifdef FEATURE_SELF_CURE_SUPPORT
81     mWifiNetLinkCallbacks.OnTcpReportMsgComplete =
82         std::bind(&WifiCommonServiceManager::OnTcpReportMsgComplete, this, _1, _2, _3);
83     WifiNetLink::GetInstance().InitWifiNetLink(mWifiNetLinkCallbacks);
84 #endif
85     if (WifiConfigCenter::GetInstance().Init() < 0) {
86         WIFI_LOGE("WifiConfigCenter Init failed!");
87         return CONFIG_CENTER_INIT_FAILED;
88     }
89     if (WifiAuthCenter::GetInstance().Init() < 0) {
90         WIFI_LOGE("WifiAuthCenter Init failed!");
91         return AUTH_CENTER_INIT_FAILED;
92     }
93 
94     if (WifiInternalEventDispatcher::GetInstance().Init() < 0) {
95         WIFI_LOGE("WifiInternalEventDispatcher Init failed!");
96         return EVENT_BROADCAST_INIT_FAILED;
97     }
98     return INIT_OK;
99 }
100 #ifndef OHOS_ARCH_LITE
OnForegroundAppChanged(const AppExecFwk::AppStateData & appStateData,const int mInstId)101 void WifiCommonServiceManager::OnForegroundAppChanged(const AppExecFwk::AppStateData &appStateData, const int mInstId)
102 {
103     IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(mInstId);
104     if (pService != nullptr) {
105         pService->HandleForegroundAppChangedAction(appStateData);
106     }
107 }
108 
OnRequestNetwork(const int uid,const int networkId)109 bool WifiCommonServiceManager::OnRequestNetwork(const int uid, const int networkId)
110 {
111     if (IsOtherVapConnect()) {
112         WIFI_LOGI("OnRequestNetwork: p2p or hml connected, and hotspot is enable");
113         WifiManager::GetInstance().GetWifiTogglerManager()->SoftapToggled(0, 0);
114     }
115     IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(0);
116     if (pService == nullptr) {
117         WIFI_LOGE("OnRequestNetwork: pService is nullptr!");
118         return false;
119     }
120     if (pService->ConnectToCandidateConfig(uid, networkId) != WIFI_OPT_SUCCESS) {
121         return false;
122     }
123     return true;
124 }
125 #endif
126 
127 #ifdef FEATURE_SELF_CURE_SUPPORT
OnTcpReportMsgComplete(const std::vector<int64_t> & elems,const int32_t cmd,const int32_t mInstId)128 void WifiCommonServiceManager::OnTcpReportMsgComplete(const std::vector<int64_t> &elems, const int32_t cmd,
129     const int32_t mInstId)
130 {
131     WIFI_LOGD("enter %{public}s", __FUNCTION__);
132     IpQosMonitor::GetInstance().HandleTcpReportMsgComplete(elems, cmd);
133 }
134 #endif
135 }  // namespace Wifi
136 }  // namespace OHOS