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 "dcamera_source_state_machine.h"
17
18 #include <memory>
19
20 #include "dcamera_source_state_factory.h"
21 #include "distributed_camera_errno.h"
22 #include "distributed_hardware_log.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
DCameraSourceStateMachine(std::shared_ptr<DCameraSourceDev> & camDev)26 DCameraSourceStateMachine::DCameraSourceStateMachine(std::shared_ptr<DCameraSourceDev>& camDev) : camDev_(camDev)
27 {
28 DHLOGI("DCameraSourceStateMachine Create");
29 }
30
~DCameraSourceStateMachine()31 DCameraSourceStateMachine::~DCameraSourceStateMachine()
32 {
33 DHLOGI("DCameraSourceStateMachine Delete");
34 }
35
Execute(DCAMERA_EVENT eventType,DCameraSourceEvent & event)36 int32_t DCameraSourceStateMachine::Execute(DCAMERA_EVENT eventType, DCameraSourceEvent& event)
37 {
38 CHECK_AND_RETURN_RET_LOG(currentState_ == nullptr, DCAMERA_BAD_VALUE,
39 "DCameraSourceStateMachine currentState_ is nullptr, please check the state machine initialization");
40 DHLOGI("In state %{public}d execute event %{public}d", currentState_->GetStateType(), eventType);
41 std::shared_ptr<DCameraSourceDev> camDev = camDev_.lock();
42 if (camDev == nullptr) {
43 DHLOGE("DCameraSourceStateMachine execute failed, camDev is nullptr");
44 return DCAMERA_BAD_VALUE;
45 }
46 auto tempState = currentState_;
47 int32_t ret = tempState->Execute(camDev, event.GetEventType(), event);
48 if (ret != DCAMERA_OK) {
49 DHLOGE("DCameraSourceStateMachine currentState_: %{public}d execute event: %{public}d failed",
50 tempState->GetStateType(), event.GetEventType());
51 }
52 return ret;
53 }
54
UpdateState(DCameraStateType stateType)55 void DCameraSourceStateMachine::UpdateState(DCameraStateType stateType)
56 {
57 if (currentState_ != nullptr && stateType != DCAMERA_STATE_INIT) {
58 DHLOGI("DCameraSourceStateMachine update state from %{public}d to %{public}d",
59 currentState_->GetStateType(), stateType);
60 } else {
61 DHLOGI("DCameraSourceStateMachine update state %{public}d", stateType);
62 }
63 auto stateMachine = std::shared_ptr<DCameraSourceStateMachine>(shared_from_this());
64 currentState_ = DCameraSourceStateFactory::GetInstance().CreateState(stateType, stateMachine);
65 }
66
GetCameraState()67 int32_t DCameraSourceStateMachine::GetCameraState()
68 {
69 CHECK_AND_RETURN_RET_LOG(currentState_ == nullptr, DCAMERA_BAD_VALUE,
70 "DCameraSourceStateMachine currentState_ is nullptr");
71 DHLOGI("GetCameraState In state %{public}d", currentState_->GetStateType());
72 return currentState_->GetStateType();
73 }
74 } // namespace DistributedHardware
75 } // namespace OHOS
76