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 #include "p2p_disabled_state.h"
16 #include "wifi_config_center.h"
17 #include "wifi_p2p_hal_interface.h"
18 #include "p2p_state_machine.h"
19 #include "wifi_logger.h"
20
21 DEFINE_WIFILOG_P2P_LABEL("P2pDisabledState");
22
23 namespace OHOS {
24 namespace Wifi {
P2pDisabledState(P2pStateMachine & stateMachine,WifiP2pGroupManager & groupMgr,WifiP2pDeviceManager & deviceMgr,WifiP2pServiceManager & svrMgr)25 P2pDisabledState::P2pDisabledState(P2pStateMachine &stateMachine, WifiP2pGroupManager &groupMgr,
26 WifiP2pDeviceManager &deviceMgr, WifiP2pServiceManager &svrMgr)
27 : State("P2pDisabledState"),
28 p2pStateMachine(stateMachine),
29 groupManager(groupMgr),
30 deviceManager(deviceMgr),
31 serviceManager(svrMgr),
32 p2pSmInitFlag(false)
33 {}
GoInState()34 void P2pDisabledState::GoInState()
35 {
36 WIFI_LOGI(" GoInState");
37 if (p2pSmInitFlag) {
38 p2pStateMachine.ClearAllP2pServiceCallbacks();
39 p2pStateMachine.groupManager.StashGroups();
40 p2pStateMachine.StopP2pDhcpClient();
41 p2pStateMachine.StopDhcpServer();
42 if (p2pStateMachine.pDhcpResultNotify != nullptr) {
43 delete p2pStateMachine.pDhcpResultNotify;
44 p2pStateMachine.pDhcpResultNotify = nullptr;
45 }
46 AbstractUI::GetInstance().UnInit();
47 }
48 p2pStateMachine.serviceManager.ClearAll();
49 deviceManager.ClearAll();
50 serviceManager.ClearAll();
51 p2pSmInitFlag = true;
52 }
53
GoOutState()54 void P2pDisabledState::GoOutState()
55 {
56 WIFI_LOGI(" GoOutState");
57 }
58
ExecuteStateMsg(InternalMessagePtr msg)59 bool P2pDisabledState::ExecuteStateMsg(InternalMessagePtr msg)
60 {
61 switch (static_cast<P2P_STATE_MACHINE_CMD>(msg->GetMessageName())) {
62 case P2P_STATE_MACHINE_CMD::CMD_P2P_ENABLE: {
63 /* WifiP2PHalInterface::createP2pInterface */
64 p2pStateMachine.p2pIface = WifiConfigCenter::GetInstance().GetP2pIfaceName();
65 p2pStateMachine.RegisterEventHandler();
66 p2pStateMachine.p2pMonitor.MonitorBegins(p2pStateMachine.p2pIface);
67 p2pStateMachine.StartTimer(
68 static_cast<int>(P2P_STATE_MACHINE_CMD::ENABLE_P2P_TIMED_OUT), ENABLE_P2P_TIMED_OUT__INTERVAL);
69 bool hasPersisentGroup = p2pStateMachine.HasPersisentGroup();
70 WIFI_LOGE("has persisen group %{public}d", hasPersisentGroup);
71 if (WifiP2PHalInterface::GetInstance().StartP2p(WifiConfigCenter::GetInstance().GetP2pIfaceName(),
72 hasPersisentGroup) == WifiErrorNo::WIFI_HAL_OPT_OK) {
73 SetVendorFeatures();
74 p2pStateMachine.SwitchState(&p2pStateMachine.p2pEnablingState);
75 } else {
76 WIFI_LOGE("StartP2p failed.");
77 p2pStateMachine.SwitchState(&p2pStateMachine.p2pEnablingState);
78 p2pStateMachine.SendMessage(static_cast<int>(P2P_STATE_MACHINE_CMD::CMD_P2P_DISABLE));
79 }
80
81 break;
82 }
83
84 default:
85 return NOT_EXECUTED;
86 }
87 return EXECUTED;
88 }
89
SetVendorFeatures() const90 void P2pDisabledState::SetVendorFeatures() const
91 {
92 P2pVendorConfig p2pVendorCfg;
93 int ret = WifiSettings::GetInstance().GetP2pVendorConfig(p2pVendorCfg);
94 if (ret < 0) {
95 WIFI_LOGW("Failed to obtain P2pVendorConfig information.");
96 }
97 #ifdef P2P_RANDOM_MAC_ADDR
98 p2pVendorCfg.SetRandomMacSupport(true);
99 WifiSettings::GetInstance().SetP2pVendorConfig(p2pVendorCfg);
100 WifiSettings::GetInstance().SyncP2pVendorConfig();
101 #endif
102 WIFI_LOGI("P2pVendorConfig random mac is %{public}s", p2pVendorCfg.GetRandomMacSupport() ? "true" : "false");
103 WifiP2PHalInterface::GetInstance().SetRandomMacAddr(p2pVendorCfg.GetRandomMacSupport());
104 }
105 } // namespace Wifi
106 } // namespace OHOS