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 return iLightHdiConnection_->GetLightList(lightInfoList_);
55 }
56
GetLightList(std::vector<LightInfoIPC> & lightList) const57 int32_t LightHdiConnection::GetLightList(std::vector<LightInfoIPC> &lightList) const
58 {
59 lightList.assign(lightInfoList_.begin(), lightInfoList_.end());
60 return ERR_OK;
61 }
62
TurnOn(int32_t lightId,const LightColor & color,const LightAnimationIPC & animation)63 int32_t LightHdiConnection::TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation)
64 {
65 CHKPR(iLightHdiConnection_, ERROR);
66 int32_t ret = iLightHdiConnection_->TurnOn(lightId, color, animation);
67 if (ret != ERR_OK) {
68 MISC_HILOGE("TurnOn failed");
69 return LIGHT_ID_NOT_SUPPORT;
70 }
71 return ERR_OK;
72 }
73
TurnOff(int32_t lightId)74 int32_t LightHdiConnection::TurnOff(int32_t lightId)
75 {
76 CHKPR(iLightHdiConnection_, ERROR);
77 int32_t ret = iLightHdiConnection_->TurnOff(lightId);
78 if (ret != ERR_OK) {
79 MISC_HILOGE("TurnOff failed");
80 return LIGHT_ERR;
81 }
82 return ERR_OK;
83 }
84
DestroyHdiConnection()85 int32_t LightHdiConnection::DestroyHdiConnection()
86 {
87 CHKPR(iLightHdiConnection_, ERROR);
88 int32_t ret = iLightHdiConnection_->DestroyHdiConnection();
89 if (ret != ERR_OK) {
90 MISC_HILOGE("DestroyHdiConnection failed");
91 return LIGHT_HDF_CONNECT_ERR;
92 }
93 return ERR_OK;
94 }
95 } // namespace Sensors
96 } // namespace OHOS
97