• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "devicestatus_srv_stub.h"
17 
18 #include <unistd.h>
19 #include <tokenid_kit.h>
20 
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23 #include "message_parcel.h"
24 #include "pixel_map.h"
25 
26 #include "devicestatus_callback_proxy.h"
27 #include "devicestatus_common.h"
28 #include "devicestatus_define.h"
29 #include "drag_data_packer.h"
30 #include "preview_style_packer.h"
31 #include "proto.h"
32 #include "stationary_callback.h"
33 #include "stationary_data.h"
34 #include "include/util.h"
35 #include "utility.h"
36 
37 namespace OHOS {
38 namespace Msdp {
39 namespace DeviceStatus {
40 namespace {
41 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DeviceStatusSrvStub" };
42 } // namespace
43 
DeviceStatusSrvStub()44 DeviceStatusSrvStub::DeviceStatusSrvStub()
45 {
46     InitCoordination();
47     InitDrag();
48 }
49 
InitCoordination()50 void DeviceStatusSrvStub::InitCoordination()
51 {
52     CALL_DEBUG_ENTER;
53     connFuncs_ = {
54         { static_cast<uint32_t>(DeviceInterfaceCode::DEVICESTATUS_SUBSCRIBE),
55             &DeviceStatusSrvStub::SubscribeStub },
56         { static_cast<uint32_t>(DeviceInterfaceCode::DEVICESTATUS_UNSUBSCRIBE),
57             &DeviceStatusSrvStub::UnsubscribeStub },
58         { static_cast<uint32_t>(DeviceInterfaceCode::DEVICESTATUS_GETCACHE),
59             &DeviceStatusSrvStub::GetLatestDeviceStatusDataStub },
60         { static_cast<uint32_t>(DeviceInterfaceCode::REGISTER_COORDINATION_MONITOR),
61             &DeviceStatusSrvStub::RegisterCoordinationMonitorStub },
62         { static_cast<uint32_t>(DeviceInterfaceCode::UNREGISTER_COORDINATION_MONITOR),
63             &DeviceStatusSrvStub::UnregisterCoordinationMonitorStub },
64         { static_cast<uint32_t>(DeviceInterfaceCode::PREPARE_COORDINATION),
65             &DeviceStatusSrvStub::PrepareCoordinationStub },
66         { static_cast<uint32_t>(DeviceInterfaceCode::UNPREPARE_COORDINATION),
67             &DeviceStatusSrvStub::UnPrepareCoordinationStub },
68         { static_cast<uint32_t>(DeviceInterfaceCode::START_COORDINATION),
69             &DeviceStatusSrvStub::ActivateCoordinationStub },
70         { static_cast<uint32_t>(DeviceInterfaceCode::STOP_COORDINATION),
71             &DeviceStatusSrvStub::DeactivateCoordinationStub },
72         { static_cast<uint32_t>(DeviceInterfaceCode::GET_COORDINATION_STATE),
73             &DeviceStatusSrvStub::GetCoordinationStateStub },
74         { static_cast<uint32_t>(DeviceInterfaceCode::ADD_HOT_AREA_MONITOR),
75             &DeviceStatusSrvStub::AddHotAreaListenerStub },
76         { static_cast<uint32_t>(DeviceInterfaceCode::REMOVE_HOT_AREA_MONITOR),
77             &DeviceStatusSrvStub::RemoveHotAreaListenerStub },
78         { static_cast<uint32_t>(DeviceInterfaceCode::REGISTER_SUBSCRIPT_MONITOR),
79             &DeviceStatusSrvStub::AddSubscriptListenerStub },
80         { static_cast<uint32_t>(DeviceInterfaceCode::UNREGISTER_SUBSCRIPT_MONITOR),
81             &DeviceStatusSrvStub::RemoveSubscriptListenerStub },
82         { static_cast<uint32_t>(DeviceInterfaceCode::REGISTER_COOPERATE_MONITOR),
83             &DeviceStatusSrvStub::RegisterCooperateMonitorStub },
84         { static_cast<uint32_t>(DeviceInterfaceCode::UNREGISTER_COOPERATE_MONITOR),
85             &DeviceStatusSrvStub::UnregisterCooperateMonitorStub },
86         { static_cast<uint32_t>(DeviceInterfaceCode::PREPARE_COOPERATE),
87             &DeviceStatusSrvStub::PrepareCooperateStub },
88         { static_cast<uint32_t>(DeviceInterfaceCode::UNPREPARE_COOPERATE),
89             &DeviceStatusSrvStub::UnPrepareCooperateStub },
90         { static_cast<uint32_t>(DeviceInterfaceCode::START_COOPERATE),
91             &DeviceStatusSrvStub::ActivateCooperateStub },
92         { static_cast<uint32_t>(DeviceInterfaceCode::STOP_COOPERATE),
93             &DeviceStatusSrvStub::DeactivateCooperateStub },
94         { static_cast<uint32_t>(DeviceInterfaceCode::GET_COOPERATE_STATE),
95             &DeviceStatusSrvStub::GetCooperateStateStub }
96     };
97 }
98 
InitDrag()99 void DeviceStatusSrvStub::InitDrag()
100 {
101     CALL_DEBUG_ENTER;
102     std::map<uint32_t, ConnFunc> dragFuncs = {
103         { static_cast<uint32_t>(DeviceInterfaceCode::ALLOC_SOCKET_FD),
104             &DeviceStatusSrvStub::HandleAllocSocketFdStub },
105         { static_cast<uint32_t>(DeviceInterfaceCode::START_DRAG),
106             &DeviceStatusSrvStub::StartDragStub },
107         { static_cast<uint32_t>(DeviceInterfaceCode::STOP_DRAG),
108             &DeviceStatusSrvStub::StopDragStub },
109         { static_cast<uint32_t>(DeviceInterfaceCode::UPDATED_DRAG_STYLE),
110             &DeviceStatusSrvStub::UpdateDragStyleStub },
111         { static_cast<uint32_t>(DeviceInterfaceCode::GET_DRAG_TARGET_PID),
112             &DeviceStatusSrvStub::GetDragTargetPidStub },
113         { static_cast<uint32_t>(DeviceInterfaceCode::GET_DRAG_TARGET_UDKEY),
114             &DeviceStatusSrvStub::GetUdKeyStub },
115         { static_cast<uint32_t>(DeviceInterfaceCode::REGISTER_DRAG_MONITOR),
116             &DeviceStatusSrvStub::AddDraglistenerStub },
117         { static_cast<uint32_t>(DeviceInterfaceCode::UNREGISTER_DRAG_MONITOR),
118             &DeviceStatusSrvStub::RemoveDraglistenerStub },
119         { static_cast<uint32_t>(DeviceInterfaceCode::SET_DRAG_WINDOW_VISIBLE),
120             &DeviceStatusSrvStub::SetDragWindowVisibleStub },
121         { static_cast<uint32_t>(DeviceInterfaceCode::GET_SHADOW_OFFSET),
122             &DeviceStatusSrvStub::GetShadowOffsetStub },
123         { static_cast<uint32_t>(DeviceInterfaceCode::UPDATE_SHADOW_PIC),
124             &DeviceStatusSrvStub::UpdateShadowPicStub },
125         { static_cast<uint32_t>(DeviceInterfaceCode::GET_DRAG_DATA),
126             &DeviceStatusSrvStub::GetDragDataStub },
127         { static_cast<uint32_t>(DeviceInterfaceCode::GET_DRAG_STATE),
128             &DeviceStatusSrvStub::GetDragStateStub },
129         { static_cast<uint32_t>(DeviceInterfaceCode::GET_DRAG_SUMMARY),
130             &DeviceStatusSrvStub::GetDragSummaryStub },
131         { static_cast<uint32_t>(DeviceInterfaceCode::ENTER_TEXT_EDITOR_AREA),
132             &DeviceStatusSrvStub::EnterTextEditorAreaStub },
133         {static_cast<uint32_t>(DeviceInterfaceCode::GET_DRAG_EXTRAINFO),
134             &DeviceStatusSrvStub::GetExtraInfoStub },
135         {static_cast<uint32_t>(DeviceInterfaceCode::GET_DRAG_ACTION),
136             &DeviceStatusSrvStub::GetDragActionStub },
137         {static_cast<uint32_t>(DeviceInterfaceCode::UPDATE_PREVIEW_STYLE),
138             &DeviceStatusSrvStub::UpdatePreviewStyleStub },
139         {static_cast<uint32_t>(DeviceInterfaceCode::UPDATE_PREVIEW_STYLE_WITH_ANIMATION),
140             &DeviceStatusSrvStub::UpdatePreviewStyleWithAnimationStub },
141         {static_cast<uint32_t>(DeviceInterfaceCode::ADD_PRIVILEGE),
142             &DeviceStatusSrvStub::AddPrivilegeStub }
143     };
144     connFuncs_.insert(dragFuncs.begin(), dragFuncs.end());
145 }
146 
CheckCooperatePermission()147 bool DeviceStatusSrvStub::CheckCooperatePermission()
148 {
149     CALL_DEBUG_ENTER;
150     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
151     int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
152         COOPERATE_PERMISSION);
153     return result == Security::AccessToken::PERMISSION_GRANTED;
154 }
155 
IsSystemServiceCalling()156 bool DeviceStatusSrvStub::IsSystemServiceCalling()
157 {
158     const auto tokenId = IPCSkeleton::GetCallingTokenID();
159     const auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
160     if (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
161         flag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
162         FI_HILOGD("system service calling, tokenId:%{public}u, flag:%{public}u", tokenId, flag);
163         return true;
164     }
165     return false;
166 }
167 
IsSystemCalling()168 bool DeviceStatusSrvStub::IsSystemCalling()
169 {
170     if (IsSystemServiceCalling()) {
171         return true;
172     }
173     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID());
174 }
175 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)176 int32_t DeviceStatusSrvStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
177     MessageOption &option)
178 {
179     FI_HILOGD("cmd:%{public}d, flags:%{public}d", code, option.GetFlags());
180     std::u16string descriptor = DeviceStatusSrvStub::GetDescriptor();
181     std::u16string remoteDescriptor = data.ReadInterfaceToken();
182     if (descriptor != remoteDescriptor) {
183         FI_HILOGE("DeviceStatusSrvStub::OnRemoteRequest failed, descriptor is not matched");
184         return E_DEVICESTATUS_GET_SERVICE_FAILED;
185     }
186     auto it = connFuncs_.find(code);
187     if (it != connFuncs_.end()) {
188         return (this->*it->second)(data, reply);
189     }
190     FI_HILOGE("Unknown code:%{public}u", code);
191     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
192 }
193 
SubscribeStub(MessageParcel & data,MessageParcel & reply)194 int32_t DeviceStatusSrvStub::SubscribeStub(MessageParcel &data, MessageParcel &reply)
195 {
196     CALL_DEBUG_ENTER;
197     int32_t type = -1;
198     READINT32(data, type, E_DEVICESTATUS_READ_PARCEL_ERROR);
199     FI_HILOGD("Read type successfully");
200     int32_t event = -1;
201     READINT32(data, event, E_DEVICESTATUS_READ_PARCEL_ERROR);
202     FI_HILOGD("Read event successfully");
203     FI_HILOGD("event:%{public}d", event);
204     int32_t latency = -1;
205     READINT32(data, latency, E_DEVICESTATUS_READ_PARCEL_ERROR);
206     FI_HILOGD("Read latency successfully");
207     sptr<IRemoteObject> obj = data.ReadRemoteObject();
208     CHKPR(obj, E_DEVICESTATUS_READ_PARCEL_ERROR);
209     FI_HILOGI("Read remote obj successfully");
210     sptr<IRemoteDevStaCallback> callback = iface_cast<IRemoteDevStaCallback>(obj);
211     CHKPR(callback, E_DEVICESTATUS_READ_PARCEL_ERROR);
212     FI_HILOGI("Read callback successfully");
213     Subscribe(static_cast<Type>(type), static_cast<ActivityEvent>(event),
214         static_cast<ReportLatencyNs>(latency), callback);
215     return RET_OK;
216 }
217 
UnsubscribeStub(MessageParcel & data,MessageParcel & reply)218 int32_t DeviceStatusSrvStub::UnsubscribeStub(MessageParcel &data, MessageParcel &reply)
219 {
220     CALL_DEBUG_ENTER;
221     int32_t type = -1;
222     READINT32(data, type, E_DEVICESTATUS_READ_PARCEL_ERROR);
223     int32_t event = -1;
224     READINT32(data, event, E_DEVICESTATUS_READ_PARCEL_ERROR);
225     FI_HILOGE("event:%{public}d", event);
226     sptr<IRemoteObject> obj = data.ReadRemoteObject();
227     CHKPR(obj, E_DEVICESTATUS_READ_PARCEL_ERROR);
228     sptr<IRemoteDevStaCallback> callback = iface_cast<IRemoteDevStaCallback>(obj);
229     CHKPR(callback, E_DEVICESTATUS_READ_PARCEL_ERROR);
230     Unsubscribe(static_cast<Type>(type), static_cast<ActivityEvent>(event), callback);
231     return RET_OK;
232 }
233 
GetLatestDeviceStatusDataStub(MessageParcel & data,MessageParcel & reply)234 int32_t DeviceStatusSrvStub::GetLatestDeviceStatusDataStub(MessageParcel &data, MessageParcel &reply)
235 {
236     CALL_DEBUG_ENTER;
237     int32_t type = -1;
238     READINT32(data, type, E_DEVICESTATUS_READ_PARCEL_ERROR);
239     Data devicestatusData = GetCache(static_cast<Type>(type));
240     FI_HILOGD("devicestatusData.type:%{public}d", devicestatusData.type);
241     FI_HILOGD("devicestatusData.value:%{public}d", devicestatusData.value);
242     WRITEINT32(reply, devicestatusData.type, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
243     WRITEINT32(reply, devicestatusData.value, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
244     return RET_OK;
245 }
246 
RegisterCoordinationMonitorStub(MessageParcel & data,MessageParcel & reply)247 int32_t DeviceStatusSrvStub::RegisterCoordinationMonitorStub(MessageParcel &data, MessageParcel &reply)
248 {
249     CALL_DEBUG_ENTER;
250     int32_t ret = RegisterCoordinationListener();
251     if (ret != RET_OK) {
252         FI_HILOGE("Call registerCoordinationEvent failed, ret:%{public}d", ret);
253     }
254     return ret;
255 }
256 
UnregisterCoordinationMonitorStub(MessageParcel & data,MessageParcel & reply)257 int32_t DeviceStatusSrvStub::UnregisterCoordinationMonitorStub(MessageParcel &data, MessageParcel &reply)
258 {
259     CALL_DEBUG_ENTER;
260     int32_t ret = UnregisterCoordinationListener();
261     if (ret != RET_OK) {
262         FI_HILOGE("Call unregisterCoordinationEvent failed, ret:%{public}d", ret);
263     }
264     return ret;
265 }
266 
PrepareCoordinationStub(MessageParcel & data,MessageParcel & reply)267 int32_t DeviceStatusSrvStub::PrepareCoordinationStub(MessageParcel &data, MessageParcel &reply)
268 {
269     CALL_DEBUG_ENTER;
270     int32_t userData = 0;
271     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
272     int32_t ret = PrepareCoordination(userData);
273     if (ret != RET_OK) {
274         FI_HILOGE("Call prepareCoordination failed, ret:%{public}d", ret);
275     }
276     return ret;
277 }
278 
UnPrepareCoordinationStub(MessageParcel & data,MessageParcel & reply)279 int32_t DeviceStatusSrvStub::UnPrepareCoordinationStub(MessageParcel &data, MessageParcel &reply)
280 {
281     CALL_DEBUG_ENTER;
282     int32_t userData = 0;
283     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
284     int32_t ret = UnprepareCoordination(userData);
285     if (ret != RET_OK) {
286         FI_HILOGE("Call unprepareCoordination failed, ret:%{public}d", ret);
287     }
288     return ret;
289 }
290 
ActivateCoordinationStub(MessageParcel & data,MessageParcel & reply)291 int32_t DeviceStatusSrvStub::ActivateCoordinationStub(MessageParcel &data, MessageParcel &reply)
292 {
293     CALL_DEBUG_ENTER;
294     int32_t userData = 0;
295     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
296     std::string remoteNetworkId;
297     READSTRING(data, remoteNetworkId, E_DEVICESTATUS_READ_PARCEL_ERROR);
298     int32_t startDeviceId = 0;
299     READINT32(data, startDeviceId, E_DEVICESTATUS_READ_PARCEL_ERROR);
300     int32_t ret = ActivateCoordination(userData, remoteNetworkId, startDeviceId);
301     if (ret != RET_OK) {
302         FI_HILOGE("Call ActivateCoordination failed, ret:%{public}d", ret);
303     }
304     return ret;
305 }
306 
DeactivateCoordinationStub(MessageParcel & data,MessageParcel & reply)307 int32_t DeviceStatusSrvStub::DeactivateCoordinationStub(MessageParcel &data, MessageParcel &reply)
308 {
309     CALL_DEBUG_ENTER;
310     int32_t userData = 0;
311     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
312     bool isUnchained = false;
313     READBOOL(data, isUnchained, E_DEVICESTATUS_READ_PARCEL_ERROR);
314     int32_t ret = DeactivateCoordination(userData, isUnchained);
315     if (ret != RET_OK) {
316         FI_HILOGE("Call DeactivateCoordination failed, ret:%{public}d", ret);
317     }
318     return ret;
319 }
320 
GetCoordinationStateStub(MessageParcel & data,MessageParcel & reply)321 int32_t DeviceStatusSrvStub::GetCoordinationStateStub(MessageParcel &data, MessageParcel &reply)
322 {
323     CALL_DEBUG_ENTER;
324     int32_t userData = 0;
325     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
326     std::string networkId;
327     READSTRING(data, networkId, E_DEVICESTATUS_READ_PARCEL_ERROR);
328     int32_t ret = GetCoordinationState(userData, networkId);
329     if (ret != RET_OK) {
330         FI_HILOGE("Call RegisterCoordinationEvent failed, ret:%{public}d", ret);
331     }
332     return ret;
333 }
334 
RegisterCooperateMonitorStub(MessageParcel & data,MessageParcel & reply)335 int32_t DeviceStatusSrvStub::RegisterCooperateMonitorStub(MessageParcel &data,
336     MessageParcel &reply)
337 {
338     CALL_DEBUG_ENTER;
339     if (!IsSystemCalling()) {
340         FI_HILOGE("The caller is not system hap");
341         return COMMON_NOT_SYSTEM_APP;
342     }
343     if (!CheckCooperatePermission()) {
344         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
345         return COMMON_PERMISSION_CHECK_ERROR;
346     }
347     int32_t ret = RegisterCoordinationListener();
348     if (ret != RET_OK) {
349         FI_HILOGE("Call registerCoordinationEvent failed, ret:%{public}d", ret);
350     }
351     return ret;
352 }
353 
UnregisterCooperateMonitorStub(MessageParcel & data,MessageParcel & reply)354 int32_t DeviceStatusSrvStub::UnregisterCooperateMonitorStub(MessageParcel &data,
355     MessageParcel &reply)
356 {
357     CALL_DEBUG_ENTER;
358     if (!IsSystemCalling()) {
359         FI_HILOGE("The caller is not system hap");
360         return COMMON_NOT_SYSTEM_APP;
361     }
362     if (!CheckCooperatePermission()) {
363         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
364         return COMMON_PERMISSION_CHECK_ERROR;
365     }
366     int32_t ret = UnregisterCoordinationListener();
367     if (ret != RET_OK) {
368         FI_HILOGE("Call unregisterCoordinationEvent failed, ret:%{public}d", ret);
369     }
370     return ret;
371 }
372 
PrepareCooperateStub(MessageParcel & data,MessageParcel & reply)373 int32_t DeviceStatusSrvStub::PrepareCooperateStub(MessageParcel &data, MessageParcel &reply)
374 {
375     CALL_DEBUG_ENTER;
376     if (!IsSystemCalling()) {
377         FI_HILOGE("The caller is not system hap");
378         return COMMON_NOT_SYSTEM_APP;
379     }
380     if (!CheckCooperatePermission()) {
381         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
382         return COMMON_PERMISSION_CHECK_ERROR;
383     }
384     int32_t userData = 0;
385     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
386     int32_t ret = PrepareCoordination(userData);
387     if (ret != RET_OK) {
388         FI_HILOGE("Call prepareCoordination failed, ret:%{public}d", ret);
389     }
390     return ret;
391 }
392 
UnPrepareCooperateStub(MessageParcel & data,MessageParcel & reply)393 int32_t DeviceStatusSrvStub::UnPrepareCooperateStub(MessageParcel &data, MessageParcel &reply)
394 {
395     CALL_DEBUG_ENTER;
396     if (!IsSystemCalling()) {
397         FI_HILOGE("The caller is not system hap");
398         return COMMON_NOT_SYSTEM_APP;
399     }
400     if (!CheckCooperatePermission()) {
401         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
402         return COMMON_PERMISSION_CHECK_ERROR;
403     }
404     int32_t userData = 0;
405     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
406     int32_t ret = UnprepareCoordination(userData);
407     if (ret != RET_OK) {
408         FI_HILOGE("Call unprepareCoordination failed, ret:%{public}d", ret);
409     }
410     return ret;
411 }
412 
ActivateCooperateStub(MessageParcel & data,MessageParcel & reply)413 int32_t DeviceStatusSrvStub::ActivateCooperateStub(MessageParcel &data, MessageParcel &reply)
414 {
415     CALL_DEBUG_ENTER;
416     if (!IsSystemCalling()) {
417         FI_HILOGE("The caller is not system hap");
418         return COMMON_NOT_SYSTEM_APP;
419     }
420     if (!CheckCooperatePermission()) {
421         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
422         return COMMON_PERMISSION_CHECK_ERROR;
423     }
424     int32_t userData = 0;
425     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
426     std::string remoteNetworkId;
427     READSTRING(data, remoteNetworkId, E_DEVICESTATUS_READ_PARCEL_ERROR);
428     int32_t startDeviceId = 0;
429     READINT32(data, startDeviceId, E_DEVICESTATUS_READ_PARCEL_ERROR);
430     int32_t ret = ActivateCoordination(userData, remoteNetworkId, startDeviceId);
431     if (ret != RET_OK) {
432         FI_HILOGE("Call ActivateCoordination failed, ret:%{public}d", ret);
433     }
434     return ret;
435 }
436 
DeactivateCooperateStub(MessageParcel & data,MessageParcel & reply)437 int32_t DeviceStatusSrvStub::DeactivateCooperateStub(MessageParcel &data, MessageParcel &reply)
438 {
439     CALL_DEBUG_ENTER;
440     if (!IsSystemCalling()) {
441         FI_HILOGE("The caller is not system hap");
442         return COMMON_NOT_SYSTEM_APP;
443     }
444     if (!CheckCooperatePermission()) {
445         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
446         return COMMON_PERMISSION_CHECK_ERROR;
447     }
448     int32_t userData = 0;
449     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
450     bool isUnchained = false;
451     READBOOL(data, isUnchained, E_DEVICESTATUS_READ_PARCEL_ERROR);
452     int32_t ret = DeactivateCoordination(userData, isUnchained);
453     if (ret != RET_OK) {
454         FI_HILOGE("Call DeactivateCoordination failed, ret:%{public}d", ret);
455     }
456     return ret;
457 }
458 
GetCooperateStateStub(MessageParcel & data,MessageParcel & reply)459 int32_t DeviceStatusSrvStub::GetCooperateStateStub(MessageParcel &data, MessageParcel &reply)
460 {
461     CALL_DEBUG_ENTER;
462     if (!IsSystemCalling()) {
463         FI_HILOGE("The caller is not system hap");
464         return COMMON_NOT_SYSTEM_APP;
465     }
466     if (!CheckCooperatePermission()) {
467         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
468         return COMMON_PERMISSION_CHECK_ERROR;
469     }
470     int32_t userData = 0;
471     READINT32(data, userData, E_DEVICESTATUS_READ_PARCEL_ERROR);
472     std::string networkId;
473     READSTRING(data, networkId, E_DEVICESTATUS_READ_PARCEL_ERROR);
474     int32_t ret = GetCoordinationState(userData, networkId);
475     if (ret != RET_OK) {
476         FI_HILOGE("Call RegisterCoordinationEvent failed, ret:%{public}d", ret);
477     }
478     return ret;
479 }
480 
UpdateDragStyleStub(MessageParcel & data,MessageParcel & reply)481 int32_t DeviceStatusSrvStub::UpdateDragStyleStub(MessageParcel &data, MessageParcel &reply)
482 {
483     int32_t style = 0;
484     READINT32(data, style, E_DEVICESTATUS_READ_PARCEL_ERROR);
485     int32_t ret = UpdateDragStyle(static_cast<DragCursorStyle>(style));
486     if (ret != RET_OK) {
487         FI_HILOGE("Call UpdateDragStyle failed, ret:%{public}d", ret);
488     }
489     return ret;
490 }
491 
GetDragTargetPidStub(MessageParcel & data,MessageParcel & reply)492 int32_t DeviceStatusSrvStub::GetDragTargetPidStub(MessageParcel &data, MessageParcel &reply)
493 {
494     int32_t pid = GetDragTargetPid();
495     WRITEINT32(reply, pid, IPC_STUB_WRITE_PARCEL_ERR);
496     return RET_OK;
497 }
498 
GetUdKeyStub(MessageParcel & data,MessageParcel & reply)499 int32_t DeviceStatusSrvStub::GetUdKeyStub(MessageParcel &data, MessageParcel &reply)
500 {
501     std::string udKey;
502     int32_t ret = GetUdKey(udKey);
503     if (ret != RET_OK) {
504         FI_HILOGE("Get udKey failed, ret:%{public}d", ret);
505     }
506     WRITESTRING(reply, udKey, IPC_STUB_WRITE_PARCEL_ERR);
507     FI_HILOGD("Target udKey:%{public}s", GetAnonyString(udKey).c_str());
508     return RET_OK;
509 }
510 
HandleAllocSocketFdStub(MessageParcel & data,MessageParcel & reply)511 int32_t DeviceStatusSrvStub::HandleAllocSocketFdStub(MessageParcel &data, MessageParcel &reply)
512 {
513     int32_t pid = GetCallingPid();
514     if (!IsRunning()) {
515         FI_HILOGE("Service is not running, pid:%{public}d, go switch default", pid);
516         return SERVICE_NOT_RUNNING;
517     }
518     int32_t moduleId = 0;
519     READINT32(data, moduleId, E_DEVICESTATUS_READ_PARCEL_ERROR);
520     std::string clientName;
521     READSTRING(data, clientName, E_DEVICESTATUS_READ_PARCEL_ERROR);
522 
523     int32_t clientFd = -1;
524     uint32_t tokenId = GetCallingTokenID();
525     int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
526     int32_t ret = AllocSocketFd(clientName, moduleId, clientFd, tokenType);
527     if (ret != RET_OK) {
528         FI_HILOGE("AllocSocketFd failed, pid:%{public}d, go switch default", pid);
529         if (clientFd >= 0) {
530             if (close(clientFd) < 0) {
531                 FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
532             }
533         }
534         return ret;
535     }
536 
537     if (!reply.WriteFileDescriptor(clientFd)) {
538         FI_HILOGE("Write file descriptor failed");
539         if (close(clientFd) < 0) {
540             FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
541         }
542         return IPC_STUB_WRITE_PARCEL_ERR;
543     }
544 
545     WRITEINT32(reply, tokenType, IPC_STUB_WRITE_PARCEL_ERR);
546     FI_HILOGD("Send clientFd to client, clientFd:%{public}d, tokenType:%{public}d", clientFd, tokenType);
547     if (close(clientFd) < 0) {
548         FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
549     }
550     return RET_OK;
551 }
552 
StartDragStub(MessageParcel & data,MessageParcel & reply)553 int32_t DeviceStatusSrvStub::StartDragStub(MessageParcel &data, MessageParcel &reply)
554 {
555     CALL_DEBUG_ENTER;
556     DragData dragData;
557     if (DragDataPacker::UnMarshalling(data, dragData) != RET_OK) {
558         FI_HILOGE("UnMarshalling dragData failed");
559         return E_DEVICESTATUS_READ_PARCEL_ERROR;
560     }
561     for (const auto& shadowInfo : dragData.shadowInfos) {
562         CHKPR(shadowInfo.pixelMap, RET_ERR);
563         if ((shadowInfo.x > 0) || (shadowInfo.y > 0) ||
564             (shadowInfo.x < -shadowInfo.pixelMap->GetWidth()) || (shadowInfo.y < -shadowInfo.pixelMap->GetHeight())) {
565             FI_HILOGE("Invalid parameter, shadowInfox:%{public}d, shadowInfoy:%{public}d", shadowInfo.x, shadowInfo.y);
566             return RET_ERR;
567         }
568     }
569     if ((dragData.dragNum <= 0) || (dragData.buffer.size() > MAX_BUFFER_SIZE) ||
570         (dragData.displayX < 0) || (dragData.displayY < 0)) {
571         FI_HILOGE("Start drag invalid parameter, dragNum:%{public}d, bufferSize:%{public}zu, "
572             "displayX:%{public}d, displayY:%{public}d",
573             dragData.dragNum, dragData.buffer.size(), dragData.displayX, dragData.displayY);
574         return RET_ERR;
575     }
576     int32_t ret = StartDrag(dragData);
577     if (ret != RET_OK) {
578         FI_HILOGE("Call StartDrag failed, ret:%{public}d", ret);
579     }
580     WRITEINT32(reply, ret, IPC_STUB_WRITE_PARCEL_ERR);
581     return ret;
582 }
583 
StopDragStub(MessageParcel & data,MessageParcel & reply)584 int32_t DeviceStatusSrvStub::StopDragStub(MessageParcel &data, MessageParcel &reply)
585 {
586     int32_t result = 0;
587     READINT32(data, result, E_DEVICESTATUS_READ_PARCEL_ERROR);
588     if ((result < static_cast<int32_t>(DragResult::DRAG_SUCCESS)) ||
589         (result > static_cast<int32_t>(DragResult::DRAG_EXCEPTION))) {
590         FI_HILOGE("Invalid result:%{public}d", result);
591         return RET_ERR;
592     }
593     bool hasCustomAnimation = false;
594     READBOOL(data, hasCustomAnimation, E_DEVICESTATUS_READ_PARCEL_ERROR);
595     int32_t windowId = -1;
596     READINT32(data, windowId, E_DEVICESTATUS_READ_PARCEL_ERROR);
597     DragDropResult dropResult;
598     dropResult.result = static_cast<DragResult>(result);
599     dropResult.hasCustomAnimation = hasCustomAnimation;
600     dropResult.windowId = windowId;
601     int32_t ret = StopDrag(dropResult);
602     if (ret != RET_OK) {
603         FI_HILOGE("Call StopDrag failed, ret:%{public}d", ret);
604     }
605     WRITEINT32(reply, ret, IPC_STUB_WRITE_PARCEL_ERR);
606     return ret;
607 }
608 
AddDraglistenerStub(MessageParcel & data,MessageParcel & reply)609 int32_t DeviceStatusSrvStub::AddDraglistenerStub(MessageParcel &data, MessageParcel &reply)
610 {
611     int32_t ret = AddDraglistener();
612     if (ret != RET_OK) {
613         FI_HILOGE("Call AddDraglistener failed, ret:%{public}d", ret);
614     }
615     return ret;
616 }
617 
RemoveDraglistenerStub(MessageParcel & data,MessageParcel & reply)618 int32_t DeviceStatusSrvStub::RemoveDraglistenerStub(MessageParcel &data, MessageParcel &reply)
619 {
620     int32_t ret = RemoveDraglistener();
621     if (ret != RET_OK) {
622         FI_HILOGE("Call RemoveDraglistener failed, ret:%{public}d", ret);
623     }
624     return ret;
625 }
626 
AddSubscriptListenerStub(MessageParcel & data,MessageParcel & reply)627 int32_t DeviceStatusSrvStub::AddSubscriptListenerStub(MessageParcel &data, MessageParcel &reply)
628 {
629     int32_t ret = AddSubscriptListener();
630     if (ret != RET_OK) {
631         FI_HILOGE("Call AddSubscriptListener failed, ret:%{public}d", ret);
632     }
633     return ret;
634 }
635 
RemoveSubscriptListenerStub(MessageParcel & data,MessageParcel & reply)636 int32_t DeviceStatusSrvStub::RemoveSubscriptListenerStub(MessageParcel &data, MessageParcel &reply)
637 {
638     int32_t ret = RemoveSubscriptListener();
639     if (ret != RET_OK) {
640         FI_HILOGE("Call RemoveSubscriptListener failed, ret:%{public}d", ret);
641     }
642     return ret;
643 }
644 
SetDragWindowVisibleStub(MessageParcel & data,MessageParcel & reply)645 int32_t DeviceStatusSrvStub::SetDragWindowVisibleStub(MessageParcel &data, MessageParcel &reply)
646 {
647     bool visible = false;
648     READBOOL(data, visible, E_DEVICESTATUS_READ_PARCEL_ERROR);
649     bool isForce = false;
650     READBOOL(data, isForce, E_DEVICESTATUS_READ_PARCEL_ERROR);
651     int32_t ret = SetDragWindowVisible(visible, isForce);
652     if (ret != RET_OK) {
653         FI_HILOGE("Call SetDragWindowVisible failed, ret:%{public}d", ret);
654     }
655     return ret;
656 }
657 
GetShadowOffsetStub(MessageParcel & data,MessageParcel & reply)658 int32_t DeviceStatusSrvStub::GetShadowOffsetStub(MessageParcel &data, MessageParcel &reply)
659 {
660     int32_t offsetX = 0;
661     int32_t offsetY = 0;
662     int32_t width = 0;
663     int32_t height = 0;
664     int32_t ret = GetShadowOffset(offsetX, offsetY, width, height);
665     if (ret != RET_OK) {
666         FI_HILOGE("Call GetShadowOffsetStub failed, ret:%{public}d", ret);
667     }
668     WRITEINT32(reply, offsetX, IPC_STUB_WRITE_PARCEL_ERR);
669     WRITEINT32(reply, offsetY, IPC_STUB_WRITE_PARCEL_ERR);
670     WRITEINT32(reply, width, IPC_STUB_WRITE_PARCEL_ERR);
671     WRITEINT32(reply, height, IPC_STUB_WRITE_PARCEL_ERR);
672     return ret;
673 }
674 
UpdateShadowPicStub(MessageParcel & data,MessageParcel & reply)675 int32_t DeviceStatusSrvStub::UpdateShadowPicStub(MessageParcel &data, MessageParcel &reply)
676 {
677     auto pixelMap = Media::PixelMap::Unmarshalling(data);
678     CHKPR(pixelMap, RET_ERR);
679     ShadowInfo shadowInfo;
680     shadowInfo.pixelMap = std::shared_ptr<OHOS::Media::PixelMap>(pixelMap);
681     READINT32(data, shadowInfo.x, E_DEVICESTATUS_READ_PARCEL_ERROR);
682     READINT32(data, shadowInfo.y, E_DEVICESTATUS_READ_PARCEL_ERROR);
683     CHKPR(shadowInfo.pixelMap, RET_ERR);
684     if ((shadowInfo.x > 0) || (shadowInfo.y > 0) ||
685         (shadowInfo.x < -shadowInfo.pixelMap->GetWidth()) ||
686         (shadowInfo.y < -shadowInfo.pixelMap->GetHeight())) {
687         FI_HILOGE("Invalid parameter, shadowInfox:%{public}d, shadowInfoy:%{public}d",
688             shadowInfo.x, shadowInfo.y);
689         return RET_ERR;
690     }
691     int32_t ret = UpdateShadowPic(shadowInfo);
692     if (ret != RET_OK) {
693         FI_HILOGE("Call Update shadow picture failed, ret:%{public}d", ret);
694     }
695     return ret;
696 }
697 
GetDragDataStub(MessageParcel & data,MessageParcel & reply)698 int32_t DeviceStatusSrvStub::GetDragDataStub(MessageParcel &data, MessageParcel &reply)
699 {
700     DragData dragData;
701     int32_t ret = GetDragData(dragData);
702     WRITEINT32(reply, ret, IPC_STUB_WRITE_PARCEL_ERR);
703 
704     if (ret != RET_OK) {
705         FI_HILOGE("Get DragData failed, ret:%{public}d", ret);
706         return RET_ERR;
707     }
708     if (DragDataPacker::Marshalling(dragData, reply) != RET_OK) {
709         FI_HILOGE("Marshalling dragData failed");
710         return RET_ERR;
711     }
712     return ret;
713 }
714 
AddHotAreaListenerStub(MessageParcel & data,MessageParcel & reply)715 int32_t DeviceStatusSrvStub::AddHotAreaListenerStub(MessageParcel &data, MessageParcel &reply)
716 {
717     CALL_DEBUG_ENTER;
718     if (!CheckCooperatePermission()) {
719         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
720         return COMMON_PERMISSION_CHECK_ERROR;
721     }
722     int32_t ret = AddHotAreaListener();
723     if (ret != RET_OK) {
724         FI_HILOGE("Call hot area listener failed, ret:%{public}d", ret);
725     }
726     return ret;
727 }
728 
GetDragStateStub(MessageParcel & data,MessageParcel & reply)729 int32_t DeviceStatusSrvStub::GetDragStateStub(MessageParcel &data, MessageParcel &reply)
730 {
731     DragState dragState;
732     int32_t ret = GetDragState(dragState);
733     if (ret != RET_OK) {
734         FI_HILOGE("Get DragState failed, ret:%{public}d", ret);
735         return RET_ERR;
736     }
737     int32_t dragStateTmp = static_cast<int32_t>(dragState);
738     WRITEINT32(reply, dragStateTmp, ERR_INVALID_VALUE);
739     return ret;
740 }
741 
RemoveHotAreaListenerStub(MessageParcel & data,MessageParcel & reply)742 int32_t DeviceStatusSrvStub::RemoveHotAreaListenerStub(MessageParcel &data, MessageParcel &reply)
743 {
744     CALL_DEBUG_ENTER;
745     if (!CheckCooperatePermission()) {
746         FI_HILOGE("The caller has no COOPERATE_MANAGER permission");
747         return COMMON_PERMISSION_CHECK_ERROR;
748     }
749     int32_t ret = RemoveHotAreaListener();
750     if (ret != RET_OK) {
751         FI_HILOGE("Call remove hot area listener failed, ret:%{public}d", ret);
752     }
753     return ret;
754 }
755 
UpdatePreviewStyleStub(MessageParcel & data,MessageParcel & reply)756 int32_t DeviceStatusSrvStub::UpdatePreviewStyleStub(MessageParcel &data, MessageParcel &reply)
757 {
758     CALL_DEBUG_ENTER;
759     PreviewStyle previewStyle;
760     if (PreviewStylePacker::UnMarshalling(data, previewStyle) != RET_OK) {
761         FI_HILOGE("UnMarshalling previewStyle failed");
762         return RET_ERR;
763     }
764     int32_t ret = UpdatePreviewStyle(previewStyle);
765     if (ret != RET_OK) {
766         FI_HILOGE("UpdatePreviewStyle failed, ret:%{public}d", ret);
767     }
768     return ret;
769 }
770 
UpdatePreviewStyleWithAnimationStub(MessageParcel & data,MessageParcel & reply)771 int32_t DeviceStatusSrvStub::UpdatePreviewStyleWithAnimationStub(MessageParcel &data, MessageParcel &reply)
772 {
773     CALL_DEBUG_ENTER;
774     PreviewStyle previewStyle;
775     if (PreviewStylePacker::UnMarshalling(data, previewStyle) != RET_OK) {
776         FI_HILOGE("UnMarshalling previewStyle failed");
777         return RET_ERR;
778     }
779     PreviewAnimation animation;
780     if (PreviewAnimationPacker::UnMarshalling(data, animation) != RET_OK) {
781         FI_HILOGE("UnMarshalling animation failed");
782         return RET_ERR;
783     }
784     int32_t ret = UpdatePreviewStyleWithAnimation(previewStyle, animation);
785     if (ret != RET_OK) {
786         FI_HILOGE("UpdatePreviewStyleWithAnimation failed, ret:%{public}d", ret);
787     }
788     return ret;
789 }
790 
GetDragSummaryStub(MessageParcel & data,MessageParcel & reply)791 int32_t DeviceStatusSrvStub::GetDragSummaryStub(MessageParcel &data, MessageParcel &reply)
792 {
793     std::map<std::string, int64_t> summarys;
794     if (GetDragSummary(summarys) != RET_OK) {
795         FI_HILOGE("Get summarys failed");
796         return RET_ERR;
797     }
798     if (SummaryPacker::Marshalling(summarys, reply) != RET_OK) {
799         FI_HILOGE("Failed to summarys unmarshalling");
800         return ERR_INVALID_VALUE;
801     }
802     return RET_OK;
803 }
804 
GetDragActionStub(MessageParcel & data,MessageParcel & reply)805 int32_t DeviceStatusSrvStub::GetDragActionStub(MessageParcel &data, MessageParcel &reply)
806 {
807     CALL_DEBUG_ENTER;
808     DragAction dragAction = DragAction::INVALID;
809     int32_t ret = GetDragAction(dragAction);
810     if (ret != RET_OK) {
811         return RET_ERR;
812     }
813     WRITEINT32(reply, static_cast<int32_t>(dragAction), IPC_STUB_WRITE_PARCEL_ERR);
814     return RET_OK;
815 }
816 
EnterTextEditorAreaStub(MessageParcel & data,MessageParcel & reply)817 int32_t DeviceStatusSrvStub::EnterTextEditorAreaStub(MessageParcel &data, MessageParcel &reply)
818 {
819     CALL_DEBUG_ENTER;
820     bool enable = false;
821     READBOOL(data, enable, E_DEVICESTATUS_READ_PARCEL_ERROR);
822     int32_t ret = EnterTextEditorArea(enable);
823     if (ret != RET_OK) {
824         FI_HILOGE("Call EnterTextEditorArea failed, ret:%{public}d", ret);
825     }
826     return ret;
827 }
828 
GetExtraInfoStub(MessageParcel & data,MessageParcel & reply)829 int32_t DeviceStatusSrvStub::GetExtraInfoStub(MessageParcel &data, MessageParcel &reply)
830 {
831     CALL_DEBUG_ENTER;
832     std::string extraInfo;
833     int32_t ret = GetExtraInfo(extraInfo);
834     if (ret != RET_OK) {
835         FI_HILOGE("Failed to get extraInfo in dragData");
836         return ret;
837     }
838     WRITESTRING(reply, extraInfo, IPC_STUB_WRITE_PARCEL_ERR);
839     return RET_OK;
840 }
841 
AddPrivilegeStub(MessageParcel & data,MessageParcel & reply)842 int32_t DeviceStatusSrvStub::AddPrivilegeStub(MessageParcel &data, MessageParcel &reply)
843 {
844     CALL_DEBUG_ENTER;
845     int32_t ret = AddPrivilege();
846     if (ret != RET_OK) {
847         FI_HILOGE("Failed to get extraInfo in dragData");
848         return ret;
849     }
850     return RET_OK;
851 }
852 } // namespace DeviceStatus
853 } // namespace Msdp
854 } // namespace OHOS