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_stub.h>
17 #include "ipc_types.h"
18 #include "iremote_object.h"
19
20 #include "mechbody_controller_log.h"
21
22 #include "mechbody_controller_types.h"
23 #include "js_mech_manager_stub.h"
24
25 namespace OHOS {
26 namespace MechBodyController {
27 namespace {
28 const std::string TAG = "JsMechManagerStub";
29 constexpr int32_t TRACK_MAX = 2;
30 }
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t JsMechManagerStub::OnRemoteRequest(uint32_t code,
33 MessageParcel &data, MessageParcel &reply, MessageOption &option)
34 {
35 auto funcIter = mechManagerFuncMap_.find(static_cast<IMechBodyControllerCode>(code));
36
37 if (funcIter != mechManagerFuncMap_.end()) {
38 MechManagerFunc func = funcIter->second;
39 std::u16string token = data.ReadInterfaceToken();
40 if (token != MECH_SERVICE_IPC_TOKEN) {
41 return IPC_TOKEN_DOES_NOT_MATCH;
42 }
43 if (func != nullptr) {
44 return (this->*func)(data, reply);
45 }
46 }
47
48 return NO_MATCHING_SERVICE_IMPL;
49 }
50
AttachStateChangeCallback(MessageParcel & data,MessageParcel & reply)51 int32_t JsMechManagerStub::AttachStateChangeCallback(MessageParcel &data,
52 MessageParcel &reply)
53 {
54 AttachmentState attachmentState = static_cast<AttachmentState>(data.ReadInt32());
55
56 std::shared_ptr<MechInfo> mechInfo(data.ReadParcelable<MechInfo>());
57 if (!mechInfo) {
58 return NAPI_RECV_DATA_FAIL;
59 }
60 return JsMechManagerService::GetInstance().AttachStateChangeCallback(attachmentState, mechInfo);
61 }
62
TrackingEventCallback(MessageParcel & data,MessageParcel & reply)63 int32_t JsMechManagerStub::TrackingEventCallback(MessageParcel &data,
64 MessageParcel &reply)
65 {
66 int32_t mechId = data.ReadInt32();
67 int32_t track = data.ReadInt32();
68 if (track > TRACK_MAX || track < 0) {
69 return NAPI_RECV_DATA_FAIL;
70 }
71
72 TrackingEvent trackingEvent = static_cast<TrackingEvent>(data.ReadInt32());
73 return JsMechManagerService::GetInstance().TrackingEventCallback(mechId, trackingEvent);
74 }
75
RotationAxesStatusChangeCallback(MessageParcel & data,MessageParcel & reply)76 int32_t JsMechManagerStub::RotationAxesStatusChangeCallback(MessageParcel &data,
77 MessageParcel &reply)
78 {
79 int32_t mechId = data.ReadInt32();
80
81 std::shared_ptr<RotationAxesStatus> rotationAxesStatus(data.ReadParcelable<RotationAxesStatus>());
82 if (!rotationAxesStatus) {
83 return NAPI_RECV_DATA_FAIL;
84 }
85
86 return JsMechManagerService::GetInstance().RotationAxesStatusChangeCallback(mechId, rotationAxesStatus);
87 }
88
RotatePromiseFulfillment(MessageParcel & data,MessageParcel & reply)89 int32_t JsMechManagerStub::RotatePromiseFulfillment(MessageParcel &data,
90 MessageParcel &reply)
91 {
92 std::string cmdId = data.ReadString();
93 int32_t result = data.ReadInt32();
94 return JsMechManagerService::GetInstance().RotatePromiseFulfillment(cmdId, result);
95 }
96
InitMechManagerFunc()97 void JsMechManagerStub::InitMechManagerFunc()
98 {
99 mechManagerFuncMap_[IMechBodyControllerCode::ATTACH_STATE_CHANGE_CALLBACK] =
100 &JsMechManagerStub::AttachStateChangeCallback;
101 mechManagerFuncMap_[IMechBodyControllerCode::TRACKING_EVENT_CALLBACK] = &JsMechManagerStub::TrackingEventCallback;
102 mechManagerFuncMap_[IMechBodyControllerCode::ROTATION_AXES_STATUS_CHANGE_CALLBACK] =
103 &JsMechManagerStub::RotationAxesStatusChangeCallback;
104 mechManagerFuncMap_[IMechBodyControllerCode::ROTATE_CALLBACK] = &JsMechManagerStub::RotatePromiseFulfillment;
105 }
106
JsMechManagerStub()107 JsMechManagerStub::JsMechManagerStub()
108 {
109 InitMechManagerFunc();
110 }
111
~JsMechManagerStub()112 JsMechManagerStub::~JsMechManagerStub()
113 {
114 HILOGE("Destry stub");
115 if (deathRecipient_ != nullptr) {
116 RemoveDeathRecipient(deathRecipient_);
117 }
118 }
119
SetDeathRecipient(sptr<IRemoteObject::DeathRecipient> deathRecipient)120 void JsMechManagerStub::SetDeathRecipient(sptr<IRemoteObject::DeathRecipient> deathRecipient)
121 {
122 if (deathRecipient_ != nullptr) {
123 RemoveDeathRecipient(deathRecipient_);
124 }
125 deathRecipient_ = deathRecipient;
126 if (deathRecipient_ != nullptr) {
127 AddDeathRecipient(deathRecipient_);
128 }
129 }
130 } // namespace MechManager
131 } // namespace OHOS