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 "flash_manager.h"
17 
18 namespace OHOS::Camera {
FlashManager()19 FlashManager::FlashManager() {}
20 
FlashManager(ManagerId managerId)21 FlashManager::FlashManager(ManagerId managerId) : IManager(managerId) {}
22 
~FlashManager()23 FlashManager::~FlashManager() {}
24 
CreateController(ControllerId controllerId,std::string hardwareName)25 RetCode FlashManager::CreateController(ControllerId controllerId, std::string hardwareName)
26 {
27     RetCode rc = RC_OK;
28     if (controllerId == DM_C_FLASH) {
29         if (!CheckCameraIdList(hardwareName)) {
30             std::shared_ptr<FlashController> flash = std::make_shared<FlashController>(hardwareName);
31             if (flash == nullptr) {
32                 return RC_ERROR;
33             }
34             rc = flash->Init();
35             if (rc == RC_OK) {
36                 flashList_.push_back(flash);
37             }
38         }
39     }
40     return rc;
41 }
42 
GetController(ControllerId controllerId,std::string hardwareName)43 std::shared_ptr<IController> FlashManager::GetController(ControllerId controllerId, std::string hardwareName)
44 {
45     (void)controllerId;
46     if (flashList_.size() == 0) {
47         return nullptr;
48     }
49     for (auto iter = flashList_.cbegin(); iter != flashList_.cend(); iter++) {
50         if ((*iter)->GetName() == hardwareName) {
51             return *iter;
52         }
53     }
54     return nullptr;
55 }
56 
Configure(std::shared_ptr<CameraMetadata> meta)57 void FlashManager::Configure(std::shared_ptr<CameraMetadata> meta)
58 {
59     if (flashList_.size() == 0) {
60         return;
61     }
62     for (auto iter = flashList_.cbegin(); iter != flashList_.cend(); iter++) {
63         (*iter)->Configure(meta);
64     }
65 };
66 
SetFlashlight(FlashMode flashMode,bool enable,std::string hardwareName)67 RetCode FlashManager::SetFlashlight(FlashMode flashMode, bool enable, std::string hardwareName)
68 {
69     for (auto iter = flashList_.cbegin(); iter != flashList_.cend(); iter++) {
70         if ((*iter)->GetName() == hardwareName) {
71             return (*iter)->SetFlashlight(flashMode, enable);
72         }
73     }
74     return RC_OK;
75 }
76 
CheckCameraIdList(std::string hardwareName)77 bool FlashManager::CheckCameraIdList(std::string hardwareName)
78 {
79     if (flashList_.size() == 0) {
80         return false;
81     }
82     for (auto iter = flashList_.cbegin(); iter != flashList_.cend(); iter++) {
83         if ((*iter)->GetName() == hardwareName) {
84             return true;
85         }
86     }
87     return false;
88 }
89 
PowerUp(std::string hardwareName)90 RetCode FlashManager::PowerUp(std::string hardwareName)
91 {
92     for (auto iter = flashList_.cbegin(); iter != flashList_.cend(); iter++) {
93         if ((*iter)->GetName() == hardwareName) {
94             return (*iter)->PowerUp();
95         }
96     }
97     return RC_ERROR;
98 }
99 
PowerDown(std::string hardwareName)100 RetCode FlashManager::PowerDown(std::string hardwareName)
101 {
102     for (auto iter = flashList_.cbegin(); iter != flashList_.cend(); iter++) {
103         if ((*iter)->GetName() == hardwareName) {
104             return (*iter)->PowerDown();
105         }
106     }
107     return RC_ERROR;
108 }
109 } // namespace OHOS::Camera