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