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 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
23 #include "hisysevent.h"
24 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
25 #include "iservice_registry.h"
26 #include "sensors_errors.h"
27 #include "system_ability_definition.h"
28
29 #undef LOG_TAG
30 #define LOG_TAG "LightClient"
31
32 namespace OHOS {
33 namespace Sensors {
34
35 namespace {
36 constexpr uint32_t MAX_LIGHT_LIST_SIZE = 0X00ff;
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 miscdeviceProxy_ = iface_cast<IMiscdeviceService>(systemManager->GetSystemAbility(
61 MISCDEVICE_SERVICE_ABILITY_ID));
62 if (miscdeviceProxy_ != nullptr) {
63 serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<LightClient *>(this));
64 CHKPR(serviceDeathObserver_, MISC_NATIVE_GET_SERVICE_ERR);
65 auto remoteObject = miscdeviceProxy_->AsObject();
66 CHKPR(remoteObject, MISC_NATIVE_GET_SERVICE_ERR);
67 remoteObject->AddDeathRecipient(serviceDeathObserver_);
68 lightInfoList_.clear();
69 miscdeviceProxy_->GetLightList(lightInfoList_);
70 return ERR_OK;
71 }
72 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
73 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_EXCEPTION",
74 HiviewDFX::HiSysEvent::EventType::FAULT, "PKG_NAME", "InitLightClient", "ERROR_CODE",
75 MISC_NATIVE_GET_SERVICE_ERR);
76 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
77 MISC_HILOGE("Get service failed");
78 return MISC_NATIVE_GET_SERVICE_ERR;
79 }
80
IsLightIdValid(int32_t lightId)81 bool LightClient::IsLightIdValid(int32_t lightId)
82 {
83 CALL_LOG_ENTER;
84 int32_t ret = InitLightClient();
85 if (ret != ERR_OK) {
86 MISC_HILOGE("InitLightClient failed, ret:%{public}d", ret);
87 return false;
88 }
89 for (const auto &item : lightInfoList_) {
90 if (lightId == item.GetLightId()) {
91 return true;
92 }
93 }
94 return false;
95 }
96
WriteHiSysIPCEvent(IMiscdeviceServiceIpcCode code,int32_t ret)97 void LightClient::WriteHiSysIPCEvent(IMiscdeviceServiceIpcCode code, int32_t ret)
98 {
99 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
100 if (ret != NO_ERROR) {
101 switch (code) {
102 case IMiscdeviceServiceIpcCode::COMMAND_TURN_ON:
103 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
104 HiviewDFX::HiSysEvent::EventType::FAULT, "PKG_NAME", "TurnOn", "ERROR_CODE", ret);
105 break;
106 case IMiscdeviceServiceIpcCode::COMMAND_TURN_OFF:
107 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
108 HiviewDFX::HiSysEvent::EventType::FAULT, "PKG_NAME", "TurnOff", "ERROR_CODE", ret);
109 break;
110 default:
111 MISC_HILOGW("Code does not exist, code:%{public}d", static_cast<int32_t>(code));
112 break;
113 }
114 }
115 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
116 }
117
GetLightList(LightInfo ** lightInfo,int32_t & count)118 int32_t LightClient::GetLightList(LightInfo **lightInfo, int32_t &count)
119 {
120 CALL_LOG_ENTER;
121 CHKPR(lightInfo, ERROR);
122 int32_t ret = InitLightClient();
123 if (ret != ERR_OK) {
124 MISC_HILOGE("InitLightClient failed");
125 return ERROR;
126 }
127 std::lock_guard<std::mutex> lightInfosLock(lightInfosMutex_);
128 if (lightInfos_ == nullptr) {
129 ret = ConvertLightInfos();
130 if (ret != ERR_OK) {
131 MISC_HILOGE("Convert light lists failed");
132 ClearLightInfos();
133 return ERROR;
134 }
135 }
136 *lightInfo = lightInfos_;
137 count = lightInfoCount_;
138 return ERR_OK;
139 }
140
IsLightAnimationValid(const LightAnimation & animation)141 bool LightClient::IsLightAnimationValid(const LightAnimation &animation)
142 {
143 CALL_LOG_ENTER;
144 if ((animation.mode < 0) || (animation.mode >= LIGHT_MODE_BUTT)) {
145 MISC_HILOGE("animation mode is invalid, mode:%{public}d", animation.mode);
146 return false;
147 }
148 if ((animation.onTime < 0) || (animation.offTime < 0)) {
149 MISC_HILOGE("animation onTime or offTime is invalid, onTime:%{public}d, offTime:%{public}d",
150 animation.onTime, animation.offTime);
151 return false;
152 }
153 return true;
154 }
155
TurnOn(int32_t lightId,const LightColor & color,const LightAnimation & animation)156 int32_t LightClient::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation)
157 {
158 CALL_LOG_ENTER;
159 if (!IsLightIdValid(lightId)) {
160 MISC_HILOGE("lightId is invalid, lightId:%{public}d", lightId);
161 return PARAMETER_ERROR;
162 }
163 if (!IsLightAnimationValid(animation)) {
164 MISC_HILOGE("animation is invalid");
165 return PARAMETER_ERROR;
166 }
167 std::lock_guard<std::mutex> clientLock(clientMutex_);
168 CHKPR(miscdeviceProxy_, ERROR);
169 LightAnimationIPC animationIPC;
170 animationIPC.SetMode(animation.mode);
171 animationIPC.SetOnTime(animation.onTime);
172 animationIPC.SetOffTime(animation.offTime);
173 int32_t ret = miscdeviceProxy_->TurnOn(lightId, color.singleColor, animationIPC);
174 WriteHiSysIPCEvent(IMiscdeviceServiceIpcCode::COMMAND_TURN_ON, ret);
175 return ret;
176 }
177
TurnOff(int32_t lightId)178 int32_t LightClient::TurnOff(int32_t lightId)
179 {
180 CALL_LOG_ENTER;
181 if (!IsLightIdValid(lightId)) {
182 MISC_HILOGE("lightId is invalid, lightId:%{public}d", lightId);
183 return LIGHT_ID_NOT_SUPPORT;
184 }
185 std::lock_guard<std::mutex> clientLock(clientMutex_);
186 CHKPR(miscdeviceProxy_, ERROR);
187 int32_t ret = miscdeviceProxy_->TurnOff(lightId);
188 WriteHiSysIPCEvent(IMiscdeviceServiceIpcCode::COMMAND_TURN_OFF, ret);
189 return ret;
190 }
191
ProcessDeathObserver(wptr<IRemoteObject> object)192 void LightClient::ProcessDeathObserver(wptr<IRemoteObject> object)
193 {
194 CALL_LOG_ENTER;
195 (void)object;
196 {
197 std::lock_guard<std::mutex> clientLock(clientMutex_);
198 miscdeviceProxy_ = nullptr;
199 }
200 auto ret = InitLightClient();
201 if (ret != ERR_OK) {
202 MISC_HILOGE("InitLightClient failed, ret:%{public}d", ret);
203 return;
204 }
205 }
206
ClearLightInfos()207 void LightClient::ClearLightInfos()
208 {
209 CALL_LOG_ENTER;
210 CHKPV(lightInfos_);
211 free(lightInfos_);
212 lightInfos_ = nullptr;
213 }
214
ConvertLightInfos()215 int32_t LightClient::ConvertLightInfos()
216 {
217 CALL_LOG_ENTER;
218 if (lightInfoList_.empty()) {
219 MISC_HILOGE("Get light lists failed");
220 return ERROR;
221 }
222 size_t count = lightInfoList_.size();
223 if (count > MAX_LIGHT_LIST_SIZE) {
224 MISC_HILOGE("The number of lights exceed the maximum value");
225 return ERROR;
226 }
227 lightInfos_ = (LightInfo *)malloc(sizeof(LightInfo) * count);
228 CHKPR(lightInfos_, ERROR);
229 for (size_t i = 0; i < count; ++i) {
230 LightInfo *lightInfo = lightInfos_ + i;
231 errno_t ret = strcpy_s(lightInfo->lightName, NAME_MAX_LEN,
232 lightInfoList_[i].GetLightName().c_str());
233 CHKCR(ret == EOK, ERROR, "strcpy_s error");
234 lightInfo->lightId = lightInfoList_[i].GetLightId();
235 lightInfo->lightNumber = lightInfoList_[i].GetLightNumber();
236 lightInfo->lightType = lightInfoList_[i].GetLightType();
237 }
238 lightInfoCount_ = static_cast<int32_t>(count);
239 return SUCCESS;
240 }
241 } // namespace Sensors
242 } // namespace OHOS
243