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 "compatible_light_connection.h"
16
17 #include "sensors_errors.h"
18
19 #undef LOG_TAG
20 #define LOG_TAG "CompatibleLightConnection"
21
22 namespace OHOS {
23 namespace Sensors {
24 std::vector<LightInfo> lightInfo = {
25 {"light_test", 1, 3, 1}
26 };
27 std::vector<int32_t> supportLights = { 1 };
28
ConnectHdi()29 int32_t CompatibleLightConnection::ConnectHdi()
30 {
31 CALL_LOG_ENTER;
32 return ERR_OK;
33 }
34
GetLightList(std::vector<LightInfoIPC> & lightList)35 int32_t CompatibleLightConnection::GetLightList(std::vector<LightInfoIPC> &lightList)
36 {
37 CALL_LOG_ENTER;
38 for (size_t i = 0; i < lightInfo.size(); ++i) {
39 LightInfoIPC light;
40 light.SetLightName(lightInfo[i].lightName);
41 light.SetLightId(lightInfo[i].lightId);
42 light.SetLightNumber(lightInfo[i].lightNumber);
43 light.SetLightType(lightInfo[i].lightType);
44 lightList.push_back(light);
45 }
46 return ERR_OK;
47 }
48
TurnOn(int32_t lightId,const LightColor & color,const LightAnimationIPC & animation)49 int32_t CompatibleLightConnection::TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation)
50 {
51 CALL_LOG_ENTER;
52 if (std::find(supportLights.begin(), supportLights.end(), lightId) == supportLights.end()) {
53 MISC_HILOGE("Not support TurnOn lightId:%{public}d", lightId);
54 return LIGHT_ID_NOT_SUPPORT;
55 }
56 int32_t mode = animation.GetMode();
57 int32_t onTime = animation.GetOnTime();
58 int32_t offTime = animation.GetOffTime();
59 if ((mode == LIGHT_MODE_BLINK || mode == LIGHT_MODE_GRADIENT) &&
60 (onTime <= 0 || offTime <= 0)) {
61 MISC_HILOGE("animation parameter error");
62 return LIGHT_ERR;
63 }
64 std::lock_guard<std::mutex> lock(turnOnLightsMutex_);
65 if (std::find(turnOnLights_.begin(), turnOnLights_.end(), lightId) != turnOnLights_.end()) {
66 MISC_HILOGI("lightId:%{public}d has been turnOn", lightId);
67 return ERR_OK;
68 }
69 turnOnLights_.push_back(lightId);
70 return ERR_OK;
71 }
72
TurnOff(int32_t lightId)73 int32_t CompatibleLightConnection::TurnOff(int32_t lightId)
74 {
75 CALL_LOG_ENTER;
76 if (std::find(supportLights.begin(), supportLights.end(), lightId) == supportLights.end()) {
77 MISC_HILOGE("Not support TurnOff lightId:%{public}d", lightId);
78 return LIGHT_ID_NOT_SUPPORT;
79 }
80 std::lock_guard<std::mutex> lock(turnOnLightsMutex_);
81 if (std::find(turnOnLights_.begin(), turnOnLights_.end(), lightId) == turnOnLights_.end()) {
82 MISC_HILOGE("lightId:%{public}d should not be turn off", lightId);
83 return LIGHT_END_ERROR;
84 }
85 std::vector<int32_t>::iterator iter = std::find(turnOnLights_.begin(), turnOnLights_.end(), lightId);
86 turnOnLights_.erase(iter);
87 return ERR_OK;
88 }
89
DestroyHdiConnection()90 int32_t CompatibleLightConnection::DestroyHdiConnection()
91 {
92 CALL_LOG_ENTER;
93 return ERR_OK;
94 }
95 } // namespace Sensors
96 } // namespace OHOS
97