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