• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
27 namespace Sensors {
28 using namespace OHOS::HiviewDFX;
29 
30 namespace {
31 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "LightHdiConnection" };
32 } // namespace
33 
ConnectHdi()34 int32_t LightHdiConnection::ConnectHdi()
35 {
36 #ifdef HDF_DRIVERS_INTERFACE_LIGHT
37     iLightHdiConnection_ = std::make_unique<HdiLightConnection>();
38     int32_t ret = ConnectHdiService();
39     if (ret == ERR_OK) {
40         MISC_HILOGI("Connect light hdi success");
41         return ERR_OK;
42     }
43 #endif // HDF_DRIVERS_INTERFACE_LIGHT
44     iLightHdiConnection_ = std::make_unique<CompatibleLightConnection>();
45     return ConnectHdiService();
46 }
47 
ConnectHdiService()48 int32_t LightHdiConnection::ConnectHdiService()
49 {
50     CHKPR(iLightHdiConnection_, ERROR);
51     int32_t ret = iLightHdiConnection_->ConnectHdi();
52     if (ret != ERR_OK) {
53         MISC_HILOGE("Connect hdi service failed");
54         return LIGHT_HDF_CONNECT_ERR;
55     }
56     return iLightHdiConnection_->GetLightList(lightInfoList_);
57 }
58 
GetLightList(std::vector<LightInfo> & lightList) const59 int32_t LightHdiConnection::GetLightList(std::vector<LightInfo>& lightList) const
60 {
61     lightList.assign(lightInfoList_.begin(), lightInfoList_.end());
62     return ERR_OK;
63 }
64 
TurnOn(int32_t lightId,const LightColor & color,const LightAnimation & animation)65 int32_t LightHdiConnection::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &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