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_response_state.h"
17
18#include "dm_auth_manager.h"
19#include "dm_constants.h"
20#include "dm_log.h"
21
22namespace OHOS {
23namespace DistributedHardware {
24int32_t AuthResponseState::Leave()
25{
26    return DM_OK;
27}
28
29int32_t AuthResponseState::SetAuthContext(std::shared_ptr<DmAuthResponseContext> context)
30{
31    context_ = std::move(context);
32    return DM_OK;
33}
34
35std::shared_ptr<DmAuthResponseContext> AuthResponseState::GetAuthContext()
36{
37    return context_;
38}
39
40int32_t AuthResponseState::SetAuthManager(std::shared_ptr<DmAuthManager> authManager)
41{
42    authManager_ = std::move(authManager);
43    return DM_OK;
44}
45
46int32_t AuthResponseState::TransitionTo(std::shared_ptr<AuthResponseState> state)
47{
48    LOGI("AuthRequestState::TransitionTo");
49    std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
50    if (stateAuthManager == nullptr) {
51        LOGE("AuthRequestState::authManager_ null");
52        return ERR_DM_FAILED;
53    }
54    if (state == nullptr) {
55        LOGE("AuthRequestState::state null");
56        return ERR_DM_FAILED;
57    }
58    if (context_ == nullptr) {
59        LOGE("AuthRequestState::Enter context_ is null");
60        return ERR_DM_FAILED;
61    }
62    state->SetAuthManager(stateAuthManager);
63    stateAuthManager->SetAuthResponseState(state);
64    state->SetAuthContext(context_);
65    this->Leave();
66    state->Enter();
67    return DM_OK;
68}
69
70int32_t AuthResponseInitState::GetStateType()
71{
72    return AuthState::AUTH_RESPONSE_INIT;
73}
74
75int32_t AuthResponseInitState::Enter()
76{
77    LOGI("AuthResponse::AuthResponseInitState Enter");
78    return DM_OK;
79}
80
81int32_t AuthResponseNegotiateState::GetStateType()
82{
83    return AuthState::AUTH_RESPONSE_NEGOTIATE;
84}
85
86int32_t AuthResponseNegotiateState::Enter()
87{
88    std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
89    if (stateAuthManager == nullptr) {
90        LOGE("AuthResponseNegotiateState::Enter authManager_ is null");
91        return ERR_DM_FAILED;
92    }
93    if (context_ == nullptr) {
94        LOGE("AuthResponseNegotiateState::Enter context_ is null");
95        return ERR_DM_FAILED;
96    }
97    stateAuthManager->RespNegotiate(context_->sessionId);
98    return DM_OK;
99}
100
101int32_t AuthResponseConfirmState::GetStateType()
102{
103    return AuthState::AUTH_RESPONSE_CONFIRM;
104}
105
106int32_t AuthResponseConfirmState::Enter()
107{
108    LOGI("AuthResponse::AuthResponseConfirmState Enter");
109    std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
110    if (stateAuthManager == nullptr) {
111        LOGE("AuthResponseConfirmState::Enter authManager_ is null");
112        return ERR_DM_FAILED;
113    }
114    stateAuthManager->ShowConfigDialog();
115    return DM_OK;
116}
117
118int32_t AuthResponseGroupState::GetStateType()
119{
120    return AuthState::AUTH_RESPONSE_GROUP;
121}
122
123int32_t AuthResponseGroupState::Enter()
124{
125    LOGI("AuthResponse::AuthResponseGroupState Enter");
126    std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
127    if (stateAuthManager == nullptr) {
128        LOGE("AuthResponseGroupState::Enter authManager_ is null");
129        return ERR_DM_FAILED;
130    }
131    stateAuthManager->CreateGroup();
132    return DM_OK;
133}
134
135int32_t AuthResponseShowState::GetStateType()
136{
137    return AuthState::AUTH_RESPONSE_SHOW;
138}
139
140int32_t AuthResponseShowState::Enter()
141{
142    std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
143    if (stateAuthManager == nullptr) {
144        LOGE("AuthResponseShowState::Enter authManager_  is null");
145        return ERR_DM_FAILED;
146    }
147    stateAuthManager->ShowAuthInfoDialog();
148    return DM_OK;
149}
150
151int32_t AuthResponseFinishState::GetStateType()
152{
153    return AuthState::AUTH_RESPONSE_FINISH;
154}
155
156int32_t AuthResponseFinishState::Enter()
157{
158    std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
159    if (stateAuthManager == nullptr) {
160        LOGE("AuthResponseFinishState::Enter authManager_  is null");
161        return ERR_DM_FAILED;
162    }
163    stateAuthManager->AuthenticateFinish();
164    return DM_OK;
165}
166} // namespace DistributedHardware
167} // namespace OHOS
168