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