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
16 #include "light_client.h"
17
18 #include <securec.h>
19 #include <thread>
20
21 #include "death_recipient_template.h"
22 #include "hisysevent.h"
23 #include "iservice_registry.h"
24 #include "sensors_errors.h"
25 #include "system_ability_definition.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "LightClient"
29
30 namespace OHOS {
31 namespace Sensors {
32
33 namespace {
34 constexpr int32_t GET_SERVICE_MAX_COUNT = 30;
35 constexpr uint32_t MAX_LIGHT_LIST_SIZE = 0X00ff;
36 constexpr uint32_t WAIT_MS = 200;
37 } // namespace
38
~LightClient()39 LightClient::~LightClient()
40 {
41 CALL_LOG_ENTER;
42 if (miscdeviceProxy_ != nullptr && serviceDeathObserver_ != nullptr) {
43 auto remoteObject = miscdeviceProxy_->AsObject();
44 if (remoteObject != nullptr) {
45 remoteObject->RemoveDeathRecipient(serviceDeathObserver_);
46 }
47 }
48 }
49
InitLightClient()50 int32_t LightClient::InitLightClient()
51 {
52 CALL_LOG_ENTER;
53 std::lock_guard<std::mutex> clientLock(clientMutex_);
54 if (miscdeviceProxy_ != nullptr) {
55 MISC_HILOGD("miscdeviceProxy_ already init");
56 return ERR_OK;
57 }
58 auto systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59 CHKPR(systemManager, MISC_NATIVE_SAM_ERR);
60 int32_t retry = 0;
61 while (retry < GET_SERVICE_MAX_COUNT) {
62 miscdeviceProxy_ = iface_cast<IMiscdeviceService>(systemManager->GetSystemAbility(
63 MISCDEVICE_SERVICE_ABILITY_ID));
64 if (miscdeviceProxy_ != nullptr) {
65 MISC_HILOGD("miscdeviceProxy_ get service success, retry:%{public}d", retry);
66 serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<LightClient *>(this));
67 CHKPR(serviceDeathObserver_, MISC_NATIVE_GET_SERVICE_ERR);
68 auto remoteObject = miscdeviceProxy_->AsObject();
69 CHKPR(remoteObject, MISC_NATIVE_GET_SERVICE_ERR);
70 remoteObject->AddDeathRecipient(serviceDeathObserver_);
71 lightInfoList_ = miscdeviceProxy_->GetLightList();
72 return ERR_OK;
73 }
74 MISC_HILOGW("Get service failed, retry:%{public}d", retry);
75 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS));
76 retry++;
77 }
78 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_EXCEPTION",
79 HiviewDFX::HiSysEvent::EventType::FAULT, "PKG_NAME", "InitLightClient", "ERROR_CODE",
80 MISC_NATIVE_GET_SERVICE_ERR);
81 MISC_HILOGE("Get service failed");
82 return MISC_NATIVE_GET_SERVICE_ERR;
83 }
84
IsLightIdValid(int32_t lightId)85 bool LightClient::IsLightIdValid(int32_t lightId)
86 {
87 CALL_LOG_ENTER;
88 int32_t ret = InitLightClient();
89 if (ret != ERR_OK) {
90 MISC_HILOGE("InitLightClient failed, ret:%{public}d", ret);
91 return false;
92 }
93 for (const auto &item : lightInfoList_) {
94 if (lightId == item.GetLightId()) {
95 return true;
96 }
97 }
98 return false;
99 }
100
GetLightList(LightInfo ** lightInfo,int32_t & count)101 int32_t LightClient::GetLightList(LightInfo **lightInfo, int32_t &count)
102 {
103 CALL_LOG_ENTER;
104 CHKPR(lightInfo, ERROR);
105 int32_t ret = InitLightClient();
106 if (ret != ERR_OK) {
107 MISC_HILOGE("InitLightClient failed");
108 return ERROR;
109 }
110 std::lock_guard<std::mutex> lightInfosLock(lightInfosMutex_);
111 if (lightInfos_ == nullptr) {
112 ret = ConvertLightInfos();
113 if (ret != ERR_OK) {
114 MISC_HILOGE("Convert light lists failed");
115 ClearLightInfos();
116 return ERROR;
117 }
118 }
119 *lightInfo = lightInfos_;
120 count = lightInfoCount_;
121 return ERR_OK;
122 }
123
IsLightAnimationValid(const LightAnimation & animation)124 bool LightClient::IsLightAnimationValid(const LightAnimation &animation)
125 {
126 CALL_LOG_ENTER;
127 if ((animation.mode < 0) || (animation.mode >= LIGHT_MODE_BUTT)) {
128 MISC_HILOGE("animation mode is invalid, mode:%{pubilc}d", animation.mode);
129 return false;
130 }
131 if ((animation.onTime < 0) || (animation.offTime < 0)) {
132 MISC_HILOGE("animation onTime or offTime is invalid, onTime:%{pubilc}d, offTime:%{pubilc}d",
133 animation.onTime, animation.offTime);
134 return false;
135 }
136 return true;
137 }
138
TurnOn(int32_t lightId,const LightColor & color,const LightAnimation & animation)139 int32_t LightClient::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation)
140 {
141 CALL_LOG_ENTER;
142 if (!IsLightIdValid(lightId)) {
143 MISC_HILOGE("lightId is invalid, lightId:%{pubilc}d", lightId);
144 return PARAMETER_ERROR;
145 }
146 if (!IsLightAnimationValid(animation)) {
147 MISC_HILOGE("animation is invalid");
148 return PARAMETER_ERROR;
149 }
150 CHKPR(miscdeviceProxy_, ERROR);
151 LightAnimationIPC animationIPC;
152 animationIPC.SetMode(animation.mode);
153 animationIPC.SetOnTime(animation.onTime);
154 animationIPC.SetOffTime(animation.offTime);
155 return miscdeviceProxy_->TurnOn(lightId, color, animationIPC);
156 }
157
TurnOff(int32_t lightId)158 int32_t LightClient::TurnOff(int32_t lightId)
159 {
160 CALL_LOG_ENTER;
161 if (!IsLightIdValid(lightId)) {
162 MISC_HILOGE("lightId is invalid, lightId:%{pubilc}d", lightId);
163 return LIGHT_ID_NOT_SUPPORT;
164 }
165 CHKPR(miscdeviceProxy_, ERROR);
166 return miscdeviceProxy_->TurnOff(lightId);
167 }
168
ProcessDeathObserver(wptr<IRemoteObject> object)169 void LightClient::ProcessDeathObserver(wptr<IRemoteObject> object)
170 {
171 CALL_LOG_ENTER;
172 (void)object;
173 miscdeviceProxy_ = nullptr;
174 auto ret = InitLightClient();
175 if (ret != ERR_OK) {
176 MISC_HILOGE("InitLightClient failed, ret:%{public}d", ret);
177 return;
178 }
179 }
180
ClearLightInfos()181 void LightClient::ClearLightInfos()
182 {
183 CALL_LOG_ENTER;
184 CHKPV(lightInfos_);
185 free(lightInfos_);
186 lightInfos_ = nullptr;
187 }
188
ConvertLightInfos()189 int32_t LightClient::ConvertLightInfos()
190 {
191 CALL_LOG_ENTER;
192 if (lightInfoList_.empty()) {
193 MISC_HILOGE("Get light lists failed");
194 return ERROR;
195 }
196 size_t count = lightInfoList_.size();
197 if (count > MAX_LIGHT_LIST_SIZE) {
198 MISC_HILOGE("The number of lights exceed the maximum value");
199 return ERROR;
200 }
201 lightInfos_ = (LightInfo *)malloc(sizeof(LightInfo) * count);
202 CHKPR(lightInfos_, ERROR);
203 for (size_t i = 0; i < count; ++i) {
204 LightInfo *lightInfo = lightInfos_ + i;
205 errno_t ret = strcpy_s(lightInfo->lightName, NAME_MAX_LEN,
206 lightInfoList_[i].GetLightName().c_str());
207 CHKCR(ret == EOK, ERROR, "strcpy_s error");
208 lightInfo->lightId = lightInfoList_[i].GetLightId();
209 lightInfo->lightNumber = lightInfoList_[i].GetLightNumber();
210 lightInfo->lightType = lightInfoList_[i].GetLightType();
211 }
212 lightInfoCount_ = static_cast<int32_t>(count);
213 return SUCCESS;
214 }
215 } // namespace Sensors
216 } // namespace OHOS
217