1 /*
2 * Copyright (c) 2025 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 "iremote_object.h"
17 #include "ipc_skeleton.h"
18 #include "iservice_registry.h"
19 #include "if_system_ability_manager.h"
20 #include "system_ability_definition.h"
21
22 #include "mechbody_controller_log.h"
23 #include "mechbody_controller_ipc_interface_code.h"
24 #include "js_mech_manager_client.h"
25
26 namespace OHOS {
27 namespace MechBodyController {
28 namespace {
29 const std::string TAG = "MechClient";
30 constexpr int32_t MAX_DEVICES = 65535;
31 constexpr int32_t DEFAULT_MECH_ID = 0;
32 }
33
AttachStateChangeListenOn(sptr<JsMechManagerStub> callback)34 int32_t MechClient::AttachStateChangeListenOn(sptr<JsMechManagerStub> callback)
35 {
36 sptr <IRemoteObject> remote = GetDmsProxy();
37 if (remote == nullptr) {
38 std::lock_guard<std::mutex> lock(systemAbilityStatusChangeListenerMutex_);
39 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40 if (samgrProxy == nullptr) {
41 return MECH_GET_SAMGR_EXCEPTION;
42 }
43 if(systemAbilityStatusChangeListener_ == nullptr){
44 systemAbilityStatusChangeListener_ = new SystemAbilityStatusChangeListener();
45 int32_t ret = samgrProxy->SubscribeSystemAbility(MECH_SERVICE_SA_ID, systemAbilityStatusChangeListener_);
46 if (ret != ERR_OK) {
47 HILOGE("SubscribeSystemAbility failed, ret:%d", ret);
48 }
49 }
50 systemAbilityStatusChangeListener_->SetCallback(callback);
51 return ERR_OK;
52 }
53 MessageParcel data;
54 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
55 return NAPI_SEND_DATA_FAIL;
56 }
57 CHECK_POINTER_RETURN_VALUE(callback, INVALID_PARAMETERS_ERR, "callback");
58 if (!data.WriteRemoteObject(callback->AsObject())) {
59 return NAPI_SEND_DATA_FAIL;
60 }
61 MessageParcel reply;
62 MessageOption option;
63 int32_t error = remote->SendRequest(
64 static_cast<int32_t>(IMechBodyControllerCode::ATTACH_STATE_CHANGE_LISTEN_ON),
65 data, reply, option);
66 if (error != ERR_NONE) {
67 return NAPI_SEND_DATA_FAIL;
68 }
69 return reply.ReadInt32();
70 }
71
AttachStateChangeListenOff()72 int32_t MechClient::AttachStateChangeListenOff()
73 {
74 sptr <IRemoteObject> remote = GetDmsProxy();
75 if (remote == nullptr) {
76 return MECH_GET_SAMGR_EXCEPTION;
77 }
78 MessageParcel data;
79 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
80 return NAPI_SEND_DATA_FAIL;
81 }
82 MessageParcel reply;
83 MessageOption option;
84 int32_t error = remote->SendRequest(
85 static_cast<int32_t>(IMechBodyControllerCode::ATTACH_STATE_CHANGE_LISTEN_OFF),
86 data, reply, option);
87 if (error != ERR_NONE) {
88 return NAPI_SEND_DATA_FAIL;
89 }
90 return reply.ReadInt32();
91 }
92
GetAttachedDevices(std::vector<std::shared_ptr<MechInfo>> & attachedDevices)93 int32_t MechClient::GetAttachedDevices(std::vector<std::shared_ptr<MechInfo>> &attachedDevices)
94 {
95 sptr <IRemoteObject> remote = GetDmsProxy();
96 if (remote == nullptr) {
97 return MECH_GET_SAMGR_EXCEPTION;
98 }
99 MessageParcel data;
100 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
101 return NAPI_SEND_DATA_FAIL;
102 }
103 MessageParcel reply;
104 MessageOption option;
105 int32_t error = remote->SendRequest(
106 static_cast<int32_t>(IMechBodyControllerCode::GET_ATTACHED_DEVICES),
107 data, reply, option);
108 if (error != ERR_NONE) {
109 return NAPI_SEND_DATA_FAIL;
110 }
111
112 int32_t result = reply.ReadInt32();
113 int32_t size = 0;
114 if (result == ERR_OK && reply.ReadInt32(size)) {
115 if (size < 0 || size > MAX_DEVICES) {
116 return NAPI_RECV_DATA_FAIL;
117 }
118 attachedDevices.reserve(size);
119 for (int32_t i = 0; i < size; ++i) {
120 std::shared_ptr<MechInfo> info(reply.ReadParcelable<MechInfo>());
121 if (!info) {
122 return NAPI_RECV_DATA_FAIL;
123 }
124 attachedDevices.emplace_back(info);
125 }
126 }
127 return result;
128 }
129
SetUserOperation(const Operation & operation,const std::string & mac,const std::string & params)130 int32_t MechClient::SetUserOperation(
131 const Operation &operation, const std::string &mac, const std::string ¶ms)
132 {
133 HILOGI("start Mech body service.");
134 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
135 if (samgrProxy == nullptr) {
136 HILOGE("Mech body service start fail: samgrProxy is nullptr.");
137 return MECH_GET_SAMGR_EXCEPTION;
138 }
139 std::shared_ptr<MechClient> mechClient = shared_from_this();
140 sptr <MechBodyServiceLoadCallback> callback(new MechBodyServiceLoadCallback(mechClient, operation, mac, params));
141 int32_t executeStartResult = samgrProxy->LoadSystemAbility(MECH_SERVICE_SA_ID, callback);
142 if (executeStartResult != ERR_OK) {
143 HILOGE("LoadSystemAbility fail, code: %{public}d", executeStartResult);
144 return START_MECH_BODY_SERVICE_FAILED;
145 }
146 {
147 std::lock_guard<std::mutex> lock(systemAbilityStatusChangeListenerMutex_);
148 if(systemAbilityStatusChangeListener_ == nullptr){
149 systemAbilityStatusChangeListener_ = new SystemAbilityStatusChangeListener();
150 int32_t ret = samgrProxy->SubscribeSystemAbility(MECH_SERVICE_SA_ID, systemAbilityStatusChangeListener_);
151 if (ret != ERR_OK) {
152 HILOGE("SubscribeSystemAbility failed, ret:%{public}d", ret);
153 }
154 }
155 }
156
157 HILOGI("Mech body service start success.");
158 return ERR_OK;
159 }
160
SendUserOperation(const Operation & operation,const std::string & mac,const std::string & params)161 int32_t MechClient::SendUserOperation(const Operation &operation, const std::string &mac, const std::string ¶ms)
162 {
163 sptr <IRemoteObject> remote = GetDmsProxy();
164 if (remote == nullptr) {
165 return MECH_GET_SAMGR_EXCEPTION;
166 }
167 MessageParcel data;
168 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
169 return NAPI_SEND_DATA_FAIL;
170 }
171 if (!data.WriteInt32(static_cast<int32_t>(operation))) {
172 return NAPI_SEND_DATA_FAIL;
173 }
174 if (!data.WriteString(mac)) {
175 return NAPI_SEND_DATA_FAIL;
176 }
177 if (!data.WriteString(params)) {
178 return NAPI_SEND_DATA_FAIL;
179 }
180 MessageParcel reply;
181 MessageOption option;
182 int32_t error = remote->SendRequest(
183 static_cast<int32_t>(IMechBodyControllerCode::SET_USER_OPERATION),
184 data, reply, option);
185 if (error != ERR_NONE) {
186 return NAPI_SEND_DATA_FAIL;
187 }
188
189 int32_t result = reply.ReadInt32();
190 HILOGI("User Operation send result: %{public}d.", result);
191 return result;
192 }
193
SetCameraTrackingEnabled(const bool & isEnabled)194 int32_t MechClient::SetCameraTrackingEnabled(const bool &isEnabled)
195 {
196 sptr <IRemoteObject> remote = GetDmsProxy();
197 if (remote == nullptr) {
198 return MECH_GET_SAMGR_EXCEPTION;
199 }
200 MessageParcel data;
201 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
202 return NAPI_SEND_DATA_FAIL;
203 }
204 if (!data.WriteBool(isEnabled)) {
205 return NAPI_SEND_DATA_FAIL;
206 }
207 MessageParcel reply;
208 MessageOption option;
209 int32_t error = remote->SendRequest(
210 static_cast<int32_t>(IMechBodyControllerCode::SET_CAMERA_TRACKING_ENABLED),
211 data, reply, option);
212 if (error != ERR_NONE) {
213 return NAPI_SEND_DATA_FAIL;
214 }
215
216 int32_t result = reply.ReadInt32();
217 return result;
218 }
219
GetCameraTrackingEnabled(bool & isEnabled)220 int32_t MechClient::GetCameraTrackingEnabled(bool &isEnabled)
221 {
222 sptr <IRemoteObject> remote = GetDmsProxy();
223 if (remote == nullptr) {
224 return MECH_GET_SAMGR_EXCEPTION;
225 }
226 MessageParcel data;
227 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
228 return NAPI_SEND_DATA_FAIL;
229 }
230 MessageParcel reply;
231 MessageOption option;
232 int32_t error = remote->SendRequest(
233 static_cast<int32_t>(IMechBodyControllerCode::GET_CAMERA_TRACKING_ENABLED),
234 data, reply, option);
235 if (error != ERR_NONE) {
236 return NAPI_SEND_DATA_FAIL;
237 }
238
239 int32_t result = reply.ReadInt32();
240 if (result == ERR_OK) {
241 isEnabled = reply.ReadBool();
242 }
243 return result;
244 }
245
TrackingEventListenOn(sptr<JsMechManagerStub> callback)246 int32_t MechClient::TrackingEventListenOn(sptr<JsMechManagerStub> callback)
247 {
248 sptr <IRemoteObject> remote = GetDmsProxy();
249 if (remote == nullptr) {
250 return MECH_GET_SAMGR_EXCEPTION;
251 }
252 MessageParcel data;
253 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
254 return NAPI_SEND_DATA_FAIL;
255 }
256 CHECK_POINTER_RETURN_VALUE(callback, INVALID_PARAMETERS_ERR, "callback");
257 if (!data.WriteRemoteObject(callback->AsObject())) {
258 return NAPI_SEND_DATA_FAIL;
259 }
260 MessageParcel reply;
261 MessageOption option;
262 int32_t error = remote->SendRequest(
263 static_cast<int32_t>(IMechBodyControllerCode::TRACKING_EVENT_LISTEN_ON),
264 data, reply, option);
265 if (error != ERR_NONE) {
266 return NAPI_SEND_DATA_FAIL;
267 }
268
269 return reply.ReadInt32();
270 }
271
TrackingEventListenOff()272 int32_t MechClient::TrackingEventListenOff()
273 {
274 sptr <IRemoteObject> remote = GetDmsProxy();
275 if (remote == nullptr) {
276 return MECH_GET_SAMGR_EXCEPTION;
277 }
278 MessageParcel data;
279 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
280 return NAPI_SEND_DATA_FAIL;
281 }
282 MessageParcel reply;
283 MessageOption option;
284 int32_t error = remote->SendRequest(
285 static_cast<int32_t>(IMechBodyControllerCode::TRACKING_EVENT_LISTEN_OFF),
286 data, reply, option);
287 if (error != ERR_NONE) {
288 return NAPI_SEND_DATA_FAIL;
289 }
290
291 return reply.ReadInt32();
292 }
293
SetCameraTrackingLayout(CameraTrackingLayout & cameraTrackingLayout)294 int32_t MechClient::SetCameraTrackingLayout(CameraTrackingLayout &cameraTrackingLayout)
295 {
296 sptr <IRemoteObject> remote = GetDmsProxy();
297 if (remote == nullptr) {
298 return MECH_GET_SAMGR_EXCEPTION;
299 }
300 MessageParcel data;
301 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
302 return NAPI_SEND_DATA_FAIL;
303 }
304 if (!data.WriteInt32(static_cast<int32_t>(cameraTrackingLayout))) {
305 return NAPI_SEND_DATA_FAIL;
306 }
307 MessageParcel reply;
308 MessageOption option;
309 int32_t error = remote->SendRequest(
310 static_cast<int32_t>(IMechBodyControllerCode::SET_CAMERA_TRACKING_LAYOUT),
311 data, reply, option);
312 if (error != ERR_NONE) {
313 return NAPI_SEND_DATA_FAIL;
314 }
315
316 return reply.ReadInt32();
317 }
318
GetCameraTrackingLayout(CameraTrackingLayout & cameraTrackingLayout)319 int32_t MechClient::GetCameraTrackingLayout(CameraTrackingLayout &cameraTrackingLayout)
320 {
321 sptr <IRemoteObject> remote = GetDmsProxy();
322 if (remote == nullptr) {
323 return MECH_GET_SAMGR_EXCEPTION;
324 }
325 MessageParcel data;
326 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
327 return NAPI_SEND_DATA_FAIL;
328 }
329 MessageParcel reply;
330 MessageOption option;
331 int32_t error = remote->SendRequest(
332 static_cast<int32_t>(IMechBodyControllerCode::GET_CAMERA_TRACKING_LAYOUT),
333 data, reply, option);
334 if (error != ERR_NONE) {
335 return NAPI_SEND_DATA_FAIL;
336 }
337
338 int32_t result = reply.ReadInt32();
339 if (result == ERR_OK) {
340 cameraTrackingLayout = static_cast<CameraTrackingLayout>(reply.ReadInt32());
341 }
342 return result;
343 }
344
RegisterCmdChannel(sptr<JsMechManagerStub> stub)345 int32_t MechClient::RegisterCmdChannel(sptr<JsMechManagerStub> stub)
346 {
347 sptr<IRemoteObject> remote = GetDmsProxy();
348 if (remote == nullptr) {
349 return MECH_GET_SAMGR_EXCEPTION;
350 }
351 MessageParcel data;
352 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
353 return NAPI_SEND_DATA_FAIL;
354 }
355 CHECK_POINTER_RETURN_VALUE(stub, INVALID_PARAMETERS_ERR, "stub");
356 if (!data.WriteRemoteObject(stub->AsObject())) {
357 return NAPI_SEND_DATA_FAIL;
358 }
359 MessageParcel reply;
360 MessageOption option;
361 int32_t error = remote->SendRequest(
362 static_cast<int32_t>(IMechBodyControllerCode::REGISTER_CMD_CHANNEL),
363 data, reply, option);
364 if (error != ERR_NONE) {
365 return NAPI_SEND_DATA_FAIL;
366 }
367 return reply.ReadInt32();
368 }
369
370
Rotate(const int32_t & mechId,const std::string & cmdId,const RotateByDegreeParam & rotateByDegreeParam)371 int32_t MechClient::Rotate(const int32_t &mechId, const std::string &cmdId,
372 const RotateByDegreeParam &rotateByDegreeParam)
373 {
374 sptr <IRemoteObject> remote = GetDmsProxy();
375 if (remote == nullptr) {
376 return MECH_GET_SAMGR_EXCEPTION;
377 }
378 MessageParcel data;
379 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
380 return NAPI_SEND_DATA_FAIL;
381 }
382 if (!data.WriteInt32(mechId)) {
383 return NAPI_SEND_DATA_FAIL;
384 }
385 if (!data.WriteString(cmdId)) {
386 return NAPI_SEND_DATA_FAIL;
387 }
388 if (!data.WriteParcelable(&rotateByDegreeParam)) {
389 return NAPI_SEND_DATA_FAIL;
390 }
391
392 MessageParcel reply;
393 MessageOption option;
394 int32_t error = remote->SendRequest(
395 static_cast<int32_t>(IMechBodyControllerCode::ROTATE_BY_DEGREE),
396 data, reply, option);
397 if (error != ERR_NONE) {
398 return NAPI_SEND_DATA_FAIL;
399 }
400 return reply.ReadInt32();
401 }
402
RotateToEulerAngles(const int32_t & mechId,const std::string & cmdId,const RotateToEulerAnglesParam & rotateToEulerAnglesParam)403 int32_t MechClient::RotateToEulerAngles(const int32_t &mechId, const std::string &cmdId,
404 const RotateToEulerAnglesParam &rotateToEulerAnglesParam)
405 {
406 sptr <IRemoteObject> remote = GetDmsProxy();
407 if (remote == nullptr) {
408 return MECH_GET_SAMGR_EXCEPTION;
409 }
410 MessageParcel data;
411 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
412 return NAPI_SEND_DATA_FAIL;
413 }
414 if (!data.WriteInt32(mechId)) {
415 return NAPI_SEND_DATA_FAIL;
416 }
417 if (!data.WriteString(cmdId)) {
418 return NAPI_SEND_DATA_FAIL;
419 }
420 if (!data.WriteParcelable(&rotateToEulerAnglesParam)) {
421 return NAPI_SEND_DATA_FAIL;
422 }
423 MessageParcel reply;
424 MessageOption option;
425 int32_t error = remote->SendRequest(
426 static_cast<int32_t>(IMechBodyControllerCode::ROTATE_TO_EULER_ANGLES),
427 data, reply, option);
428 if (error != ERR_NONE) {
429 return NAPI_SEND_DATA_FAIL;
430 }
431 return reply.ReadInt32();
432 }
433
GetMaxRotationTime(const int32_t & mechId,TimeLimit & timeLimit)434 int32_t MechClient::GetMaxRotationTime(const int32_t &mechId, TimeLimit &timeLimit)
435 {
436 sptr <IRemoteObject> remote = GetDmsProxy();
437 if (remote == nullptr) {
438 return MECH_GET_SAMGR_EXCEPTION;
439 }
440 MessageParcel data;
441 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
442 return NAPI_SEND_DATA_FAIL;
443 }
444 if (!data.WriteInt32(mechId)) {
445 return NAPI_SEND_DATA_FAIL;
446 }
447 MessageParcel reply;
448 MessageOption option;
449 int32_t error = remote->SendRequest(
450 static_cast<int32_t>(IMechBodyControllerCode::GET_MAX_ROTATION_TIME),
451 data, reply, option);
452 if (error != ERR_NONE) {
453 return NAPI_SEND_DATA_FAIL;
454 }
455
456 int32_t result = reply.ReadInt32();
457 if (result == ERR_OK) {
458 timeLimit.min = reply.ReadFloat();
459 timeLimit.max = reply.ReadFloat();
460 }
461 return result;
462 }
463
GetMaxRotationSpeed(const int32_t & mechId,RotateSpeedLimit & rotateSpeedLimit)464 int32_t MechClient::GetMaxRotationSpeed(const int32_t &mechId, RotateSpeedLimit &rotateSpeedLimit)
465 {
466 sptr <IRemoteObject> remote = GetDmsProxy();
467 if (remote == nullptr) {
468 return MECH_GET_SAMGR_EXCEPTION;
469 }
470 MessageParcel data;
471 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
472 return NAPI_SEND_DATA_FAIL;
473 }
474 if (!data.WriteInt32(mechId)) {
475 return NAPI_SEND_DATA_FAIL;
476 }
477 MessageParcel reply;
478 MessageOption option;
479 int32_t error = remote->SendRequest(
480 static_cast<int32_t>(IMechBodyControllerCode::GET_MAX_ROTATION_SPEED),
481 data, reply, option);
482 if (error != ERR_NONE) {
483 return NAPI_SEND_DATA_FAIL;
484 }
485
486 int32_t result = reply.ReadInt32();
487 if (result == ERR_OK) {
488 std::shared_ptr<RotateSpeedLimit> speedLimit(reply.ReadParcelable<RotateSpeedLimit>());
489 if (speedLimit == nullptr) {
490 return result;
491 }
492 rotateSpeedLimit = *speedLimit;
493 }
494 return result;
495 }
496
RotateBySpeed(const int32_t & mechId,const std::string & cmdId,RotateBySpeedParam & rotateBySpeedParam)497 int32_t MechClient::RotateBySpeed(const int32_t &mechId, const std::string &cmdId,
498 RotateBySpeedParam &rotateBySpeedParam)
499 {
500 sptr <IRemoteObject> remote = GetDmsProxy();
501 if (remote == nullptr) {
502 return MECH_GET_SAMGR_EXCEPTION;
503 }
504 MessageParcel data;
505 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
506 return NAPI_SEND_DATA_FAIL;
507 }
508 if (!data.WriteInt32(mechId)) {
509 return NAPI_SEND_DATA_FAIL;
510 }
511 if (!data.WriteString(cmdId)) {
512 return NAPI_SEND_DATA_FAIL;
513 }
514 if (!data.WriteParcelable(&rotateBySpeedParam)) {
515 return NAPI_SEND_DATA_FAIL;
516 }
517 MessageParcel reply;
518 MessageOption option;
519 int32_t error = remote->SendRequest(
520 static_cast<int32_t>(IMechBodyControllerCode::ROTATE_BY_SPEED),
521 data, reply, option);
522 if (error != ERR_NONE) {
523 return NAPI_SEND_DATA_FAIL;
524 }
525
526 return reply.ReadInt32();
527 }
528
StopMoving(const int32_t & mechId,const std::string & cmdId)529 int32_t MechClient::StopMoving(const int32_t &mechId, const std::string &cmdId)
530 {
531 sptr <IRemoteObject> remote = GetDmsProxy();
532 if (remote == nullptr) {
533 return MECH_GET_SAMGR_EXCEPTION;
534 }
535 MessageParcel data;
536 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
537 return NAPI_SEND_DATA_FAIL;
538 }
539 if (!data.WriteString(cmdId)) {
540 return NAPI_SEND_DATA_FAIL;
541 }
542 if (!data.WriteInt32(mechId)) {
543 return NAPI_SEND_DATA_FAIL;
544 }
545 MessageParcel reply;
546 MessageOption option;
547 int32_t error = remote->SendRequest(
548 static_cast<int32_t>(IMechBodyControllerCode::STOP_MOVING),
549 data, reply, option);
550 if (error != ERR_NONE) {
551 return NAPI_SEND_DATA_FAIL;
552 }
553 return reply.ReadInt32();
554 }
555
GetRotationAngles(const int32_t & mechId,EulerAngles & eulerAngles)556 int32_t MechClient::GetRotationAngles(const int32_t &mechId, EulerAngles &eulerAngles)
557 {
558 sptr <IRemoteObject> remote = GetDmsProxy();
559 if (remote == nullptr) {
560 return MECH_GET_SAMGR_EXCEPTION;
561 }
562 MessageParcel data;
563 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
564 return NAPI_SEND_DATA_FAIL;
565 }
566 if (!data.WriteInt32(mechId)) {
567 return NAPI_SEND_DATA_FAIL;
568 }
569 MessageParcel reply;
570 MessageOption option;
571 int32_t error = remote->SendRequest(
572 static_cast<int32_t>(IMechBodyControllerCode::GET_ROTATION_ANGLES),
573 data, reply, option);
574 if (error != ERR_NONE) {
575 return NAPI_SEND_DATA_FAIL;
576 }
577
578 int32_t result = reply.ReadInt32();
579 if (result == ERR_OK) {
580 std::shared_ptr<EulerAngles> currentPositionResult(reply.ReadParcelable<EulerAngles>());
581 if (currentPositionResult == nullptr) {
582 return result;
583 }
584 eulerAngles = *currentPositionResult;
585 }
586 return result;
587 }
588
GetRotationDegreeLimits(const int32_t & mechId,RotateDegreeLimit & rotationLimit)589 int32_t MechClient::GetRotationDegreeLimits(const int32_t &mechId, RotateDegreeLimit &rotationLimit)
590 {
591 sptr <IRemoteObject> remote = GetDmsProxy();
592 if (remote == nullptr) {
593 return MECH_GET_SAMGR_EXCEPTION;
594 }
595 MessageParcel data;
596 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
597 return NAPI_SEND_DATA_FAIL;
598 }
599 if (!data.WriteInt32(mechId)) {
600 return NAPI_SEND_DATA_FAIL;
601 }
602 MessageParcel reply;
603 MessageOption option;
604 int32_t error = remote->SendRequest(
605 static_cast<int32_t>(IMechBodyControllerCode::GET_ROTATION_DEGREE_LIMITS),
606 data, reply, option);
607 if (error != ERR_NONE) {
608 return NAPI_SEND_DATA_FAIL;
609 }
610
611 int32_t result = reply.ReadInt32();
612 if (result == ERR_OK) {
613 std::shared_ptr<RotateDegreeLimit> rotationLimitResult(reply.ReadParcelable<RotateDegreeLimit>());
614 if (rotationLimitResult == nullptr) {
615 return result;
616 }
617 rotationLimit = *rotationLimitResult;
618 }
619 return result;
620 }
621
GetRotationAxesStatus(const int32_t & mechId,RotationAxesStatus & axesStatus)622 int32_t MechClient::GetRotationAxesStatus(const int32_t &mechId, RotationAxesStatus &axesStatus)
623 {
624 sptr <IRemoteObject> remote = GetDmsProxy();
625 if (remote == nullptr) {
626 return MECH_GET_SAMGR_EXCEPTION;
627 }
628 MessageParcel data;
629 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
630 return NAPI_SEND_DATA_FAIL;
631 }
632 if (!data.WriteInt32(mechId)) {
633 return NAPI_SEND_DATA_FAIL;
634 }
635 MessageParcel reply;
636 MessageOption option;
637 int32_t error = remote->SendRequest(
638 static_cast<int32_t>(IMechBodyControllerCode::GET_ROTATION_AXES_STATUS),
639 data, reply, option);
640 if (error != ERR_NONE) {
641 return NAPI_SEND_DATA_FAIL;
642 }
643
644 int32_t result = reply.ReadInt32();
645 if (result == ERR_OK) {
646 std::shared_ptr<RotationAxesStatus> axesStatusResult(reply.ReadParcelable<RotationAxesStatus>());
647 if (axesStatusResult == nullptr) {
648 return result;
649 }
650 axesStatus = *axesStatusResult;
651 }
652 return result;
653 }
654
RotationAxesStatusChangeListenOn(sptr<JsMechManagerStub> callback)655 int32_t MechClient::RotationAxesStatusChangeListenOn(sptr<JsMechManagerStub> callback)
656 {
657 sptr <IRemoteObject> remote = GetDmsProxy();
658 if (remote == nullptr) {
659 return MECH_GET_SAMGR_EXCEPTION;
660 }
661 MessageParcel data;
662 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
663 return NAPI_SEND_DATA_FAIL;
664 }
665 CHECK_POINTER_RETURN_VALUE(callback, INVALID_PARAMETERS_ERR, "callback");
666 if (!data.WriteRemoteObject(callback->AsObject())) {
667 return NAPI_SEND_DATA_FAIL;
668 }
669 MessageParcel reply;
670 MessageOption option;
671 int32_t error = remote->SendRequest(
672 static_cast<int32_t>(IMechBodyControllerCode::ROTATION_AXES_STATUS_CHANGE_LISTEN_ON),
673 data, reply, option);
674 if (error != ERR_NONE) {
675 return NAPI_SEND_DATA_FAIL;
676 }
677
678 return reply.ReadInt32();
679 }
680
RotationAxesStatusChangeListenOff()681 int32_t MechClient::RotationAxesStatusChangeListenOff()
682 {
683 sptr <IRemoteObject> remote = GetDmsProxy();
684 if (remote == nullptr) {
685 return MECH_GET_SAMGR_EXCEPTION;
686 }
687 MessageParcel data;
688 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
689 return NAPI_SEND_DATA_FAIL;
690 }
691 MessageParcel reply;
692 MessageOption option;
693 int32_t error = remote->SendRequest(
694 static_cast<int32_t>(IMechBodyControllerCode::ROTATION_AXES_STATUS_CHANGE_LISTEN_OFF),
695 data, reply, option);
696 if (error != ERR_NONE) {
697 return NAPI_SEND_DATA_FAIL;
698 }
699
700 return reply.ReadInt32();
701 }
702
GetDmsProxy()703 sptr <IRemoteObject> MechClient::GetDmsProxy()
704 {
705 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
706 if (samgrProxy == nullptr) {
707 return nullptr;
708 }
709
710 sptr <IRemoteObject> proxy = samgrProxy->CheckSystemAbility(MECH_SERVICE_SA_ID);
711 if (proxy != nullptr) {
712 return proxy;
713 }
714
715 return nullptr;
716 }
717
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)718 void SystemAbilityStatusChangeListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
719 {
720 if (systemAbilityId != MECH_SERVICE_SA_ID) {
721 HILOGI("MechService SA:%{public}d added, but not mech body SA", systemAbilityId);
722 return;
723 }
724 HILOGI("MechService SA:%{public}d added", systemAbilityId);
725 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
726 if (samgrProxy == nullptr) {
727 HILOGE("get samanager proxy failed;");
728 return;
729 }
730
731 sptr <IRemoteObject> remote = samgrProxy->CheckSystemAbility(MECH_SERVICE_SA_ID);
732 if (remote == nullptr) {
733 HILOGE("get mech body proxy failed;");
734 return;
735 }
736
737 MessageParcel data;
738 if (!data.WriteInterfaceToken(MECH_SERVICE_IPC_TOKEN)) {
739 return;
740 }
741 CHECK_POINTER_RETURN(callback_, "callback_");
742 if (!data.WriteRemoteObject(callback_->AsObject())) {
743 return;
744 }
745 MessageParcel reply;
746 MessageOption option;
747 int32_t error = remote->SendRequest(
748 static_cast<int32_t>(IMechBodyControllerCode::ATTACH_STATE_CHANGE_LISTEN_ON),
749 data, reply, option);
750 if (error != ERR_NONE) {
751 return;
752 }
753 HILOGI("AttachStateChangeListenOn result: %{public}d", reply.ReadInt32());
754 }
755
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)756 void SystemAbilityStatusChangeListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
757 {
758 if (systemAbilityId != MECH_SERVICE_SA_ID) {
759 return;
760 }
761 std::shared_ptr<MechInfo> mechInfo = std::make_shared<MechInfo>();
762 mechInfo->mechId = DEFAULT_MECH_ID;
763 JsMechManagerService::GetInstance().AttachStateChangeCallback(AttachmentState::DETACHED, mechInfo);
764 HILOGI("MechService SA:%{public}d removed", systemAbilityId);
765 }
766
SetCallback(const sptr<JsMechManagerStub> & callback)767 void SystemAbilityStatusChangeListener::SetCallback(const sptr <JsMechManagerStub> &callback) {
768 callback_ = callback;
769 }
770
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)771 void MechBodyServiceLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
772 const sptr <IRemoteObject> &remoteObject)
773 {
774 HILOGI("OnLoadSystemAbilitySuccess systemAbilityId: %{public}d, IRmoteObject result: %{public}s",
775 systemAbilityId, (remoteObject != nullptr) ? "true" : "false");
776 if (systemAbilityId != MECH_SERVICE_SA_ID) {
777 HILOGE("start systemabilityId is not sourceSAId!");
778 return;
779 }
780 if (remoteObject == nullptr) {
781 HILOGE("remoteObject is null.");
782 return;
783 }
784 std::shared_ptr<MechClient> mechClient = client.lock();
785 if(mechClient == nullptr){
786 return;
787 }
788 mechClient->SendUserOperation(operation, mac, param);
789 }
790
OnLoadSystemAbilityFail(int32_t systemAbilityId)791 void MechBodyServiceLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
792 {
793 HILOGI("OnLoadSystemAbilityFail systemAbilityId: %d.", systemAbilityId);
794 if (systemAbilityId != MECH_SERVICE_SA_ID) {
795 HILOGE("start systemabilityId is not sourceSAId!");
796 return;
797 }
798 std::shared_ptr<MechClient> mechClient = client.lock();
799 if(mechClient == nullptr){
800 return;
801 }
802 }
803
MechBodyServiceLoadCallback(const std::weak_ptr<MechClient> & client,Operation operation,const std::string & mac,const std::string & param)804 MechBodyServiceLoadCallback::MechBodyServiceLoadCallback(const std::weak_ptr<MechClient> &client,
805 Operation operation, const std::string &mac, const std::string ¶m) : client(client),
806 operation(operation), mac(mac), param(param) {}
807
808 } // namespace MechManager
809 } // namespace OHOS