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     DHLOGI("In state %{public}d execute event %{public}d", currentState_->GetStateType(), eventType);
39     std::shared_ptr<DCameraSourceDev> camDev = camDev_.lock();
40     if (camDev == nullptr) {
41         DHLOGE("DCameraSourceStateMachine execute failed, camDev is nullptr");
42         return DCAMERA_BAD_VALUE;
43     }
44     int32_t ret = currentState_->Execute(camDev, event.GetEventType(), event);
45     if (ret != DCAMERA_OK) {
46         DHLOGE("DCameraSourceStateMachine currentState_: %{public}d execute event: %{public}d failed",
47             currentState_->GetStateType(), event.GetEventType());
48     }
49     return ret;
50 }
51 
UpdateState(DCameraStateType stateType)52 void DCameraSourceStateMachine::UpdateState(DCameraStateType stateType)
53 {
54     if (stateType != DCAMERA_STATE_INIT) {
55         DHLOGI("DCameraSourceStateMachine update state from %{public}d to %{public}d",
56             currentState_->GetStateType(), stateType);
57     } else {
58         DHLOGI("DCameraSourceStateMachine update state %{public}d", stateType);
59     }
60     auto stateMachine = std::shared_ptr<DCameraSourceStateMachine>(shared_from_this());
61     currentState_ = DCameraSourceStateFactory::GetInstance().CreateState(stateType, stateMachine);
62 }
63 
GetCameraState()64 int32_t DCameraSourceStateMachine::GetCameraState()
65 {
66     DHLOGI("GetCameraState In state %{public}d", currentState_->GetStateType());
67     return currentState_->GetStateType();
68 }
69 } // namespace DistributedHardware
70 } // namespace OHOS
71