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 16 #include "auth_request_state.h" 17 18 #include "dm_auth_manager.h" 19 #include "dm_constants.h" 20 21 namespace OHOS { 22 namespace DistributedHardware { Leave()23int32_t AuthRequestState::Leave() 24 { 25 return DM_OK; 26 } 27 SetAuthManager(std::shared_ptr<DmAuthManager> authManager)28int32_t AuthRequestState::SetAuthManager(std::shared_ptr<DmAuthManager> authManager) 29 { 30 authManager_ = std::move(authManager); 31 return DM_OK; 32 } 33 SetAuthContext(std::shared_ptr<DmAuthRequestContext> context)34int32_t AuthRequestState::SetAuthContext(std::shared_ptr<DmAuthRequestContext> context) 35 { 36 context_ = std::move(context); 37 return DM_OK; 38 } 39 GetAuthContext()40std::shared_ptr<DmAuthRequestContext> AuthRequestState::GetAuthContext() 41 { 42 return context_; 43 } 44 TransitionTo(std::shared_ptr<AuthRequestState> state)45int32_t AuthRequestState::TransitionTo(std::shared_ptr<AuthRequestState> state) 46 { 47 LOGE("AuthRequestState::TransitionTo"); 48 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 49 if (stateAuthManager == nullptr) { 50 LOGE("AuthRequestState::authManager_ null"); 51 return DM_FAILED; 52 } 53 state->SetAuthManager(stateAuthManager); 54 stateAuthManager->SetAuthRequestState(state); 55 state->SetAuthContext(context_); 56 this->Leave(); 57 state->Enter(); 58 return DM_OK; 59 } 60 GetStateType()61int32_t AuthRequestNegotiateState::GetStateType() 62 { 63 return AuthState::AUTH_REQUEST_NEGOTIATE; 64 } 65 Enter()66int32_t AuthRequestNegotiateState::Enter() 67 { 68 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 69 if (stateAuthManager == nullptr) { 70 LOGE("AuthRequestState::authManager_ null"); 71 return DM_FAILED; 72 } 73 stateAuthManager->StartNegotiate(context_->sessionId); 74 return DM_OK; 75 } 76 GetStateType()77int32_t AuthRequestNegotiateDoneState::GetStateType() 78 { 79 return AuthState::AUTH_REQUEST_NEGOTIATE_DONE; 80 } 81 Enter()82int32_t AuthRequestNegotiateDoneState::Enter() 83 { 84 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 85 if (stateAuthManager == nullptr) { 86 LOGE("AuthRequestState::authManager_ null"); 87 return DM_FAILED; 88 } 89 stateAuthManager->SendAuthRequest(context_->sessionId); 90 return DM_OK; 91 } 92 GetStateType()93int32_t AuthRequestReplyState::GetStateType() 94 { 95 return AuthState::AUTH_REQUEST_REPLY; 96 } 97 Enter()98int32_t AuthRequestReplyState::Enter() 99 { 100 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 101 if (stateAuthManager == nullptr) { 102 LOGE("AuthRequestState::authManager_ null"); 103 return DM_FAILED; 104 } 105 stateAuthManager->StartRespAuthProcess(); 106 return DM_OK; 107 } 108 GetStateType()109int32_t AuthRequestInputState::GetStateType() 110 { 111 return AuthState::AUTH_REQUEST_INPUT; 112 } 113 Enter()114int32_t AuthRequestInputState::Enter() 115 { 116 LOGE("DmAuthManager::AuthRequestInputState"); 117 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 118 if (stateAuthManager == nullptr) { 119 LOGE("AuthRequestState::authManager_ null"); 120 return DM_FAILED; 121 } 122 stateAuthManager->ShowStartAuthDialog(); 123 return DM_OK; 124 } 125 GetStateType()126int32_t AuthRequestJoinState::GetStateType() 127 { 128 return AuthState::AUTH_REQUEST_JOIN; 129 } 130 Enter()131int32_t AuthRequestJoinState::Enter() 132 { 133 LOGE("DmAuthManager::AuthRequestJoinState"); 134 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 135 if (stateAuthManager == nullptr) { 136 LOGE("AuthRequestState::authManager_ null"); 137 return DM_FAILED; 138 } 139 stateAuthManager->AddMember(context_->deviceId); 140 return DM_OK; 141 } 142 GetStateType()143int32_t AuthRequestNetworkState::GetStateType() 144 { 145 return AuthState::AUTH_REQUEST_NETWORK; 146 } 147 Enter()148int32_t AuthRequestNetworkState::Enter() 149 { 150 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 151 if (stateAuthManager == nullptr) { 152 LOGE("AuthRequestState::authManager_ null"); 153 return DM_FAILED; 154 } 155 stateAuthManager->JoinNetwork(); 156 return DM_OK; 157 } 158 GetStateType()159int32_t AuthRequestFinishState::GetStateType() 160 { 161 return AuthState::AUTH_REQUEST_FINISH; 162 } 163 Enter()164int32_t AuthRequestFinishState::Enter() 165 { 166 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 167 if (stateAuthManager == nullptr) { 168 LOGE("AuthRequestState::authManager_ null"); 169 return DM_FAILED; 170 } 171 stateAuthManager->AuthenticateFinish(); 172 return DM_OK; 173 } 174 } // namespace DistributedHardware 175 } // namespace OHOS 176