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 #include "light_hdi_connection.h"
16 
17 #include <list>
18 
19 #include "compatible_light_connection.h"
20 #ifdef HDF_DRIVERS_INTERFACE_LIGHT
21 #include "hdi_light_connection.h"
22 #endif // HDF_DRIVERS_INTERFACE_LIGHT
23 #include "hitrace_meter.h"
24 #include "sensors_errors.h"
25 
26 #undef LOG_TAG
27 #define LOG_TAG "LightHdiConnection"
28 
29 namespace OHOS {
30 namespace Sensors {
31 
ConnectHdi()32 int32_t LightHdiConnection::ConnectHdi()
33 {
34 #ifdef HDF_DRIVERS_INTERFACE_LIGHT
35     iLightHdiConnection_ = std::make_unique<HdiLightConnection>();
36     int32_t ret = ConnectHdiService();
37     if (ret == ERR_OK) {
38         MISC_HILOGI("Connect light hdi success");
39         return ERR_OK;
40     }
41 #endif // HDF_DRIVERS_INTERFACE_LIGHT
42     iLightHdiConnection_ = std::make_unique<CompatibleLightConnection>();
43     return ConnectHdiService();
44 }
45 
ConnectHdiService()46 int32_t LightHdiConnection::ConnectHdiService()
47 {
48     CHKPR(iLightHdiConnection_, ERROR);
49     int32_t ret = iLightHdiConnection_->ConnectHdi();
50     if (ret != ERR_OK) {
51         MISC_HILOGE("Connect hdi service failed");
52         return LIGHT_HDF_CONNECT_ERR;
53     }
54     std::lock_guard<std::mutex> lightInfoListLock(lightInfoListMutex_);
55     return iLightHdiConnection_->GetLightList(lightInfoList_);
56 }
57 
GetLightList(std::vector<LightInfoIPC> & lightList)58 int32_t LightHdiConnection::GetLightList(std::vector<LightInfoIPC> &lightList)
59 {
60     std::lock_guard<std::mutex> lightInfoListLock(lightInfoListMutex_);
61     lightList.assign(lightInfoList_.begin(), lightInfoList_.end());
62     return ERR_OK;
63 }
64 
TurnOn(int32_t lightId,const LightColor & color,const LightAnimationIPC & animation)65 int32_t LightHdiConnection::TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation)
66 {
67     CHKPR(iLightHdiConnection_, ERROR);
68     int32_t ret = iLightHdiConnection_->TurnOn(lightId, color, animation);
69     if (ret != ERR_OK) {
70         MISC_HILOGE("TurnOn failed");
71         return LIGHT_ID_NOT_SUPPORT;
72     }
73     return ERR_OK;
74 }
75 
TurnOff(int32_t lightId)76 int32_t LightHdiConnection::TurnOff(int32_t lightId)
77 {
78     CHKPR(iLightHdiConnection_, ERROR);
79     int32_t ret = iLightHdiConnection_->TurnOff(lightId);
80     if (ret != ERR_OK) {
81         MISC_HILOGE("TurnOff failed");
82         return LIGHT_ERR;
83     }
84     return ERR_OK;
85 }
86 
DestroyHdiConnection()87 int32_t LightHdiConnection::DestroyHdiConnection()
88 {
89     CHKPR(iLightHdiConnection_, ERROR);
90     int32_t ret = iLightHdiConnection_->DestroyHdiConnection();
91     if (ret != ERR_OK) {
92         MISC_HILOGE("DestroyHdiConnection failed");
93         return LIGHT_HDF_CONNECT_ERR;
94     }
95     return ERR_OK;
96 }
97 }  // namespace Sensors
98 }  // namespace OHOS
99