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_group_join_state.h"
16 #include "wifi_p2p_hal_interface.h"
17 #include "p2p_state_machine.h"
18 #include "wifi_logger.h"
19 
20 DEFINE_WIFILOG_P2P_LABEL("P2pGroupJoinState");
21 
22 namespace OHOS {
23 namespace Wifi {
P2pGroupJoinState(P2pStateMachine & stateMachine,WifiP2pGroupManager & groupMgr,WifiP2pDeviceManager & deviceMgr)24 P2pGroupJoinState::P2pGroupJoinState(
25     P2pStateMachine &stateMachine, WifiP2pGroupManager &groupMgr, WifiP2pDeviceManager &deviceMgr)
26     : State("P2pGroupJoinState"), p2pStateMachine(stateMachine), groupManager(groupMgr), deviceManager(deviceMgr)
27 {}
GoInState()28 void P2pGroupJoinState::GoInState()
29 {
30     WIFI_LOGI("GoInState");
31     p2pStateMachine.NotifyUserInvitationReceivedMessage();
32     const int exceptionTimeOut = 120000;
33     p2pStateMachine.MessageExecutedLater(
34         static_cast<int>(P2P_STATE_MACHINE_CMD::INTERNAL_CONN_USER_TIME_OUT), exceptionTimeOut);
35 }
36 
GoOutState()37 void P2pGroupJoinState::GoOutState()
38 {
39     WIFI_LOGI("GoOutState");
40     p2pStateMachine.StopTimer(static_cast<int>(P2P_STATE_MACHINE_CMD::INTERNAL_CONN_USER_TIME_OUT));
41 }
42 
ExecuteStateMsg(InternalMessagePtr msg)43 bool P2pGroupJoinState::ExecuteStateMsg(InternalMessagePtr msg)
44 {
45     switch (static_cast<P2P_STATE_MACHINE_CMD>(msg->GetMessageName())) {
46         case P2P_STATE_MACHINE_CMD::INTERNAL_CONN_USER_ACCEPT: {
47             WIFI_LOGI("user accept.");
48             const WpsInfo &wps = p2pStateMachine.savedP2pConfig.GetWpsInfo();
49             if (wps.GetWpsMethod() == WpsMethod::WPS_METHOD_KEYPAD) {
50                 std::string inputPin;
51                 if (!msg->GetMessageObj(inputPin)) {
52                     WIFI_LOGW("Failed to obtain the pin code.");
53                     break;
54                 }
55                 WpsInfo wpsPin = wps;
56                 wpsPin.SetPin(inputPin);
57                 WIFI_LOGI("INPUT PIN: [%{private}s] ", wpsPin.GetPin().c_str());
58                 p2pStateMachine.savedP2pConfig.SetWpsInfo(wpsPin);
59             }
60             if (WifiErrorNo::WIFI_HAL_OPT_OK != WifiP2PHalInterface::GetInstance().P2pStopFind()) {
61                 WIFI_LOGE("Failed to stop find.");
62             }
63             StartPbc(wps);
64             p2pStateMachine.SwitchState(&p2pStateMachine.p2pGroupFormedState);
65             break;
66         }
67         case P2P_STATE_MACHINE_CMD::PEER_CONNECTION_USER_REJECT: {
68             WIFI_LOGI("User(GO) rejected to join the group.");
69             p2pStateMachine.SwitchState(&p2pStateMachine.p2pGroupFormedState);
70             break;
71         }
72         case P2P_STATE_MACHINE_CMD::CMD_P2P_DISABLE: {
73             p2pStateMachine.DelayMessage(msg);
74             p2pStateMachine.SwitchState(&p2pStateMachine.p2pGroupFormedState);
75             break;
76         }
77         case P2P_STATE_MACHINE_CMD::INTERNAL_CONN_USER_TIME_OUT: {
78             p2pStateMachine.SwitchState(&p2pStateMachine.p2pGroupFormedState);
79             break;
80         }
81         default:
82             return NOT_EXECUTED;
83     }
84     return EXECUTED;
85 }
86 
StartPbc(const WpsInfo & wps)87 void P2pGroupJoinState::StartPbc(const WpsInfo &wps)
88 {
89     const WifiP2pGroupInfo group = groupManager.GetCurrentGroup();
90     std::string pin = wps.GetPin();
91     std::string result;
92     std::string address;
93     if (wps.GetWpsMethod() == WpsMethod::WPS_METHOD_PBC) {
94 #ifdef HDI_WPA_INTERFACE_SUPPORT
95         if (WifiErrorNo::WIFI_HAL_OPT_OK !=
96             WifiP2PHalInterface::GetInstance().StartWpsPbc(group.GetInterface(),
97                 p2pStateMachine.savedP2pConfig.GetDeviceAddress())) {
98 #else
99             if (WifiErrorNo::WIFI_HAL_OPT_OK !=
100                 WifiP2PHalInterface::GetInstance().StartWpsPbc(group.GetInterface(), pin)) {
101 #endif
102                     WIFI_LOGE("WpsPbc operation failed.");
103                 }
104     } else {
105         if (WifiErrorNo::WIFI_HAL_OPT_OK !=
106             WifiP2PHalInterface::GetInstance().StartWpsPin(group.GetInterface(), address, pin, result)) {
107             WIFI_LOGE("WpsPin operation failed.");
108         }
109     }
110 }
111 } // namespace Wifi
112 } // namespace OHOS
113