1 /* 2 * Copyright (c) 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 "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 LOGI("AuthRequestState::TransitionTo"); 48 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 49 if (stateAuthManager == nullptr) { 50 LOGE("AuthRequestState::authManager_ null"); 51 return ERR_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 AuthRequestInitState::GetStateType() 62 { 63 return AuthState::AUTH_REQUEST_INIT; 64 } 65 Enter()66int32_t AuthRequestInitState::Enter() 67 { 68 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 69 if (stateAuthManager == nullptr) { 70 LOGE("AuthRequestState::authManager_ null"); 71 return ERR_DM_FAILED; 72 } 73 stateAuthManager->EstablishAuthChannel(context_->deviceId); 74 return DM_OK; 75 } 76 GetStateType()77int32_t AuthRequestNegotiateState::GetStateType() 78 { 79 return AuthState::AUTH_REQUEST_NEGOTIATE; 80 } 81 Enter()82int32_t AuthRequestNegotiateState::Enter() 83 { 84 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 85 if (stateAuthManager == nullptr) { 86 LOGE("AuthRequestState::authManager_ null"); 87 return ERR_DM_FAILED; 88 } 89 stateAuthManager->StartNegotiate(context_->sessionId); 90 return DM_OK; 91 } 92 GetStateType()93int32_t AuthRequestNegotiateDoneState::GetStateType() 94 { 95 return AuthState::AUTH_REQUEST_NEGOTIATE_DONE; 96 } 97 Enter()98int32_t AuthRequestNegotiateDoneState::Enter() 99 { 100 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 101 if (stateAuthManager == nullptr) { 102 LOGE("AuthRequestState::authManager_ null"); 103 return ERR_DM_FAILED; 104 } 105 stateAuthManager->SendAuthRequest(context_->sessionId); 106 return DM_OK; 107 } 108 GetStateType()109int32_t AuthRequestReplyState::GetStateType() 110 { 111 return AuthState::AUTH_REQUEST_REPLY; 112 } 113 Enter()114int32_t AuthRequestReplyState::Enter() 115 { 116 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 117 if (stateAuthManager == nullptr) { 118 LOGE("AuthRequestState::authManager_ null"); 119 return ERR_DM_FAILED; 120 } 121 stateAuthManager->StartRespAuthProcess(); 122 return DM_OK; 123 } 124 GetStateType()125int32_t AuthRequestJoinState::GetStateType() 126 { 127 return AuthState::AUTH_REQUEST_JOIN; 128 } 129 Enter()130int32_t AuthRequestJoinState::Enter() 131 { 132 LOGI("DmAuthManager::AuthRequestJoinState"); 133 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 134 if (stateAuthManager == nullptr) { 135 LOGE("AuthRequestState::authManager_ null"); 136 return ERR_DM_FAILED; 137 } 138 stateAuthManager->ShowStartAuthDialog(); 139 return DM_OK; 140 } 141 GetStateType()142int32_t AuthRequestNetworkState::GetStateType() 143 { 144 return AuthState::AUTH_REQUEST_NETWORK; 145 } 146 Enter()147int32_t AuthRequestNetworkState::Enter() 148 { 149 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 150 if (stateAuthManager == nullptr) { 151 LOGE("AuthRequestState::authManager_ null"); 152 return ERR_DM_FAILED; 153 } 154 stateAuthManager->JoinNetwork(); 155 return DM_OK; 156 } 157 GetStateType()158int32_t AuthRequestFinishState::GetStateType() 159 { 160 return AuthState::AUTH_REQUEST_FINISH; 161 } 162 Enter()163int32_t AuthRequestFinishState::Enter() 164 { 165 std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock(); 166 if (stateAuthManager == nullptr) { 167 LOGE("AuthRequestState::authManager_ null"); 168 return ERR_DM_FAILED; 169 } 170 stateAuthManager->AuthenticateFinish(); 171 return DM_OK; 172 } 173 } // namespace DistributedHardware 174 } // namespace OHOS 175