• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     auto tempState = currentState_;
45     int32_t ret = tempState->Execute(camDev, event.GetEventType(), event);
46     if (ret != DCAMERA_OK) {
47         DHLOGE("DCameraSourceStateMachine currentState_: %{public}d execute event: %{public}d failed",
48             tempState->GetStateType(), event.GetEventType());
49     }
50     return ret;
51 }
52 
UpdateState(DCameraStateType stateType)53 void DCameraSourceStateMachine::UpdateState(DCameraStateType stateType)
54 {
55     if (stateType != DCAMERA_STATE_INIT) {
56         DHLOGI("DCameraSourceStateMachine update state from %{public}d to %{public}d",
57             currentState_->GetStateType(), stateType);
58     } else {
59         DHLOGI("DCameraSourceStateMachine update state %{public}d", stateType);
60     }
61     auto stateMachine = std::shared_ptr<DCameraSourceStateMachine>(shared_from_this());
62     currentState_ = DCameraSourceStateFactory::GetInstance().CreateState(stateType, stateMachine);
63 }
64 
GetCameraState()65 int32_t DCameraSourceStateMachine::GetCameraState()
66 {
67     DHLOGI("GetCameraState In state %{public}d", currentState_->GetStateType());
68     return currentState_->GetStateType();
69 }
70 } // namespace DistributedHardware
71 } // namespace OHOS
72