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 <memory>
18
19 #include "mechbody_controller_stub.h"
20 #include "mechbody_controller_ipc_interface_code.h"
21 #include "mechbody_controller_log.h"
22 #include "mechbody_controller_service.h"
23
24 namespace OHOS {
25 namespace MechBodyController {
26 namespace {
27 const std::string TAG = "MechBodyControllerStub";
28 }
29
MechBodyControllerStub()30 MechBodyControllerStub::MechBodyControllerStub()
31 {
32 InitFuncsInner();
33 }
34
~MechBodyControllerStub()35 MechBodyControllerStub::~MechBodyControllerStub()
36 {
37 funcsMap_.clear();
38 }
39
InitFuncsInner()40 void MechBodyControllerStub::InitFuncsInner()
41 {
42 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::ATTACH_STATE_CHANGE_LISTEN_ON)] =
43 &MechBodyControllerStub::AttachStateChangeListenOnInner;
44 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::ATTACH_STATE_CHANGE_LISTEN_OFF)] =
45 &MechBodyControllerStub::AttachStateChangeListenOffInner;
46 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::GET_ATTACHED_DEVICES)] =
47 &MechBodyControllerStub::GetAttachedDevicesInner;
48 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::SET_USER_OPERATION)] =
49 &MechBodyControllerStub::SetUserOperationInner;
50 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::SET_CAMERA_TRACKING_ENABLED)] =
51 &MechBodyControllerStub::SetCameraTrackingEnabledInner;
52 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::GET_CAMERA_TRACKING_ENABLED)] =
53 &MechBodyControllerStub::GetCameraTrackingEnabledInner;
54 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::TRACKING_EVENT_LISTEN_ON)] =
55 &MechBodyControllerStub::TrackingEventListenOnInner;
56 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::TRACKING_EVENT_LISTEN_OFF)] =
57 &MechBodyControllerStub::TrackingEventListenOffInner;
58 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::SET_CAMERA_TRACKING_LAYOUT)] =
59 &MechBodyControllerStub::SetCameraTrackingLayoutInner;
60 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::GET_CAMERA_TRACKING_LAYOUT)] =
61 &MechBodyControllerStub::GetCameraTrackingLayoutInner;
62 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::REGISTER_CMD_CHANNEL)] =
63 &MechBodyControllerStub::RegisterCmdChannelInner;
64 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::ROTATE_BY_DEGREE)] =
65 &MechBodyControllerStub::RotateByDegreeInner;
66 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::ROTATE_TO_EULER_ANGLES)] =
67 &MechBodyControllerStub::RotateToEulerAnglesInner;
68 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::GET_MAX_ROTATION_TIME)] =
69 &MechBodyControllerStub::GetMaxRotationTimeInner;
70 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::GET_MAX_ROTATION_SPEED)] =
71 &MechBodyControllerStub::GetMaxRotationSpeedInner;
72 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::ROTATE_BY_SPEED)] =
73 &MechBodyControllerStub::RotateBySpeedInner;
74 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::STOP_MOVING)] =
75 &MechBodyControllerStub::StopMovingInner;
76 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::GET_ROTATION_ANGLES)] =
77 &MechBodyControllerStub::GetRotationAnglesInner;
78 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::GET_ROTATION_DEGREE_LIMITS)] =
79 &MechBodyControllerStub::GetRotationDegreeLimitsInner;
80 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::GET_ROTATION_AXES_STATUS)] =
81 &MechBodyControllerStub::GetRotationAxesStatusInner;
82 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::ROTATION_AXES_STATUS_CHANGE_LISTEN_ON)] =
83 &MechBodyControllerStub::RotationAxesStatusChangeListenOnInner;
84 funcsMap_[static_cast<uint32_t>(IMechBodyControllerCode::ROTATION_AXES_STATUS_CHANGE_LISTEN_OFF)] =
85 &MechBodyControllerStub::RotationAxesStatusChangeListenOffInner;
86 }
87
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)88 int32_t MechBodyControllerStub::OnRemoteRequest(
89 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
90 {
91 HILOGI("OnRemoteRequest, code = %{public}u, flags = %{public}d", code, option.GetFlags());
92 auto iter = funcsMap_.find(code);
93 if (iter != funcsMap_.end()) {
94 auto func = iter->second;
95 std::u16string interfaceToken = data.ReadInterfaceToken();
96 if (interfaceToken != MECH_SERVICE_IPC_TOKEN) {
97 HILOGW("OnRemoteRequest interface token check failed!");
98 return INVALID_PARAMETERS_ERR;
99 }
100 if (func != nullptr) {
101 return (this->*func)(data, reply);
102 }
103 }
104
105 HILOGW("OnRemoteRequest default case, need check.");
106 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
107 }
108
109
AttachStateChangeListenOnInner(MessageParcel & data,MessageParcel & reply)110 int32_t MechBodyControllerStub::AttachStateChangeListenOnInner(MessageParcel &data, MessageParcel &reply)
111 {
112 sptr <IRemoteObject> callback = data.ReadRemoteObject();
113
114 int32_t result = MechBodyControllerService::GetInstance().RegisterAttachStateChangeCallback(callback);
115 bool writeResult = reply.WriteInt32(result);
116 HILOGI("%{public}d", writeResult);
117 return result;
118 }
119
AttachStateChangeListenOffInner(MessageParcel & data,MessageParcel & reply)120 int32_t MechBodyControllerStub::AttachStateChangeListenOffInner(MessageParcel &data, MessageParcel &reply)
121 {
122 int32_t result = MechBodyControllerService::GetInstance().UnRegisterAttachStateChangeCallback();
123 bool writeResult = reply.WriteInt32(result);
124 HILOGI("%{public}d", result);
125 HILOGI("%{public}d", writeResult);
126 return result;
127 }
128
GetAttachedDevicesInner(MessageParcel & data,MessageParcel & reply)129 int32_t MechBodyControllerStub::GetAttachedDevicesInner(MessageParcel &data, MessageParcel &reply)
130 {
131 std::set<MechInfo> mechInfos;
132 int32_t result = MechBodyControllerService::GetInstance().GetAttachedDevices(mechInfos);
133 bool writeResult = reply.WriteInt32(result);
134 if (result == ERR_OK && writeResult) {
135 reply.WriteInt32(mechInfos.size());
136 for (const auto &item : mechInfos) {
137 HILOGE("mech id: %{public}d", item.mechId);
138 reply.WriteParcelable(&item);
139 }
140 }
141 return result;
142 }
143
SetUserOperationInner(MessageParcel & data,MessageParcel & reply)144 int32_t MechBodyControllerStub::SetUserOperationInner(MessageParcel &data, MessageParcel &reply)
145 {
146 std::shared_ptr<Operation> operation = std::make_shared<Operation>(
147 static_cast<Operation>(data.ReadInt32()));
148 std::string mac = data.ReadString();
149 std::string param = data.ReadString();
150 return MechBodyControllerService::GetInstance().SetUserOperation(operation, mac, param);
151 }
152
SetCameraTrackingEnabledInner(MessageParcel & data,MessageParcel & reply)153 int32_t MechBodyControllerStub::SetCameraTrackingEnabledInner(MessageParcel &data, MessageParcel &reply)
154 {
155 bool isEnable = data.ReadBool();
156 int32_t result = MechBodyControllerService::GetInstance().SetTrackingEnabled(isEnable);
157 bool writeResult = reply.WriteInt32(result);
158 HILOGI("%{public}d", writeResult);
159 return result;
160 }
161
GetCameraTrackingEnabledInner(MessageParcel & data,MessageParcel & reply)162 int32_t MechBodyControllerStub::GetCameraTrackingEnabledInner(MessageParcel &data, MessageParcel &reply)
163 {
164 bool isEnable;
165 int32_t result = MechBodyControllerService::GetInstance().GetTrackingEnabled(isEnable);
166 bool writeResult = reply.WriteInt32(result);
167 if (result == ERR_OK) {
168 writeResult = reply.WriteBool(isEnable);
169 }
170 HILOGI("%{public}d", writeResult);
171 return result;
172 }
173
TrackingEventListenOnInner(MessageParcel & data,MessageParcel & reply)174 int32_t MechBodyControllerStub::TrackingEventListenOnInner(MessageParcel &data, MessageParcel &reply)
175 {
176 sptr <IRemoteObject> callback = data.ReadRemoteObject();
177 int32_t result = MechBodyControllerService::GetInstance().RegisterTrackingEventCallback(callback);
178 bool writeResult = reply.WriteInt32(result);
179 HILOGI("result %{public}d", result);
180 HILOGI("writeResult %{public}d", writeResult);
181 return result;
182 }
183
TrackingEventListenOffInner(MessageParcel & data,MessageParcel & reply)184 int32_t MechBodyControllerStub::TrackingEventListenOffInner(MessageParcel &data, MessageParcel &reply)
185 {
186 sptr <IRemoteObject> callback = data.ReadRemoteObject();
187 int32_t result = MechBodyControllerService::GetInstance().UnRegisterTrackingEventCallback();
188 bool writeResult = reply.WriteInt32(result);
189 HILOGI("result %{public}d", result);
190 HILOGI("writeResult %{public}d", writeResult);
191 return result;
192 }
193
SetCameraTrackingLayoutInner(MessageParcel & data,MessageParcel & reply)194 int32_t MechBodyControllerStub::SetCameraTrackingLayoutInner(MessageParcel &data, MessageParcel &reply)
195 {
196 auto trackingLayout = static_cast<CameraTrackingLayout>(data.ReadInt32());
197 int32_t result = MechBodyControllerService::GetInstance().SetTrackingLayout(trackingLayout);
198 bool writeResult = reply.WriteInt32(result);
199 HILOGI("%{public}d", writeResult);
200 return result;
201 }
202
GetCameraTrackingLayoutInner(MessageParcel & data,MessageParcel & reply)203 int32_t MechBodyControllerStub::GetCameraTrackingLayoutInner(MessageParcel &data, MessageParcel &reply)
204 {
205 CameraTrackingLayout trackingLayout = CameraTrackingLayout::DEFAULT;
206 int32_t result = MechBodyControllerService::GetInstance().GetTrackingLayout(trackingLayout);
207 HILOGI("GetMechCameraTrackingLayout : %{public}d", static_cast<int32_t>(trackingLayout));
208 bool writeResult = reply.WriteInt32(result);
209 if (result == ERR_OK) {
210 writeResult = reply.WriteInt32(static_cast<int32_t>(trackingLayout));
211 }
212 HILOGI("%{public}d", writeResult);
213 return result;
214 }
215
RegisterCmdChannelInner(MessageParcel & data,MessageParcel & reply)216 int32_t MechBodyControllerStub::RegisterCmdChannelInner(MessageParcel &data, MessageParcel &reply)
217 {
218 sptr <IRemoteObject> callback = data.ReadRemoteObject();
219 int32_t result = MechBodyControllerService::GetInstance().RegisterCmdChannel(callback);
220 bool writeResult = reply.WriteInt32(result);
221 HILOGI("%{public}d", writeResult);
222 return result;
223 }
224
RotateByDegreeInner(MessageParcel & data,MessageParcel & reply)225 int32_t MechBodyControllerStub::RotateByDegreeInner(MessageParcel &data, MessageParcel &reply)
226 {
227 int32_t mechId = data.ReadInt32();
228 std::string cmdId = data.ReadString();
229 std::shared_ptr<RotateByDegreeParam> rotateByDegreeParam(data.ReadParcelable<RotateByDegreeParam>());
230 sptr <IRemoteObject> callback = data.ReadRemoteObject();
231 int32_t result = MechBodyControllerService::GetInstance().RotateByDegree(mechId, cmdId, rotateByDegreeParam);
232 bool writeResult = reply.WriteInt32(result);
233 HILOGI("%{public}d", writeResult);
234 return result;
235 }
236
RotateToEulerAnglesInner(MessageParcel & data,MessageParcel & reply)237 int32_t MechBodyControllerStub::RotateToEulerAnglesInner(MessageParcel &data, MessageParcel &reply)
238 {
239 int32_t mechId = data.ReadInt32();
240 std::string cmdId = data.ReadString();
241 std::shared_ptr<RotateToEulerAnglesParam> rotateToEulerAnglesParam(
242 data.ReadParcelable<RotateToEulerAnglesParam>());
243 sptr <IRemoteObject> callback = data.ReadRemoteObject();
244 int32_t result = MechBodyControllerService::GetInstance()
245 .RotateToEulerAngles(mechId, cmdId, rotateToEulerAnglesParam);
246 bool writeResult = reply.WriteInt32(result);
247 HILOGI("%{public}d", writeResult);
248 return result;
249 }
250
GetMaxRotationTimeInner(MessageParcel & data,MessageParcel & reply)251 int32_t MechBodyControllerStub::GetMaxRotationTimeInner(MessageParcel &data, MessageParcel &reply)
252 {
253 int32_t mechId = data.ReadInt32();
254 std::shared_ptr<TimeLimit> timeLimit = std::make_shared<TimeLimit>();
255 int32_t result = MechBodyControllerService::GetInstance().GetMaxRotationTime(mechId, timeLimit);
256 bool writeResult = reply.WriteInt32(result);
257 HILOGI("result: %{public}d; max: %{public}f;", result, timeLimit->max);
258 if (result == ERR_OK) {
259 writeResult = reply.WriteFloat(timeLimit->min);
260 writeResult = reply.WriteFloat(timeLimit->max);
261 }
262 HILOGI("%{public}d", writeResult);
263 return result;
264 }
265
GetMaxRotationSpeedInner(MessageParcel & data,MessageParcel & reply)266 int32_t MechBodyControllerStub::GetMaxRotationSpeedInner(MessageParcel &data, MessageParcel &reply)
267 {
268 int32_t mechId = data.ReadInt32();
269 RotateSpeedLimit speedLimit;
270 int32_t result = MechBodyControllerService::GetInstance().GetMaxRotationSpeed(mechId, speedLimit);
271 bool writeResult = reply.WriteInt32(result);
272 if (result == ERR_OK) {
273 HILOGE("RotateSpeedLimit: %{public}s", speedLimit.ToString().c_str());
274 reply.WriteParcelable(&speedLimit);
275 }
276 HILOGI("%{public}d", writeResult);
277 return result;
278 }
279
RotateBySpeedInner(MessageParcel & data,MessageParcel & reply)280 int32_t MechBodyControllerStub::RotateBySpeedInner(MessageParcel &data, MessageParcel &reply)
281 {
282 int32_t mechId = data.ReadInt32();
283 std::string cmdId = data.ReadString();
284 std::shared_ptr<RotateBySpeedParam> rotateBySpeedParam(data.ReadParcelable<RotateBySpeedParam>());
285 sptr <IRemoteObject> callback = data.ReadRemoteObject();
286 int32_t result = MechBodyControllerService::GetInstance().RotateBySpeed(mechId, cmdId, rotateBySpeedParam);
287 bool writeResult = reply.WriteInt32(result);
288 HILOGI("%{public}d", writeResult);
289 return result;
290 }
291
StopMovingInner(MessageParcel & data,MessageParcel & reply)292 int32_t MechBodyControllerStub::StopMovingInner(MessageParcel &data, MessageParcel &reply)
293 {
294 std::string cmdId = data.ReadString();
295 int32_t mechId = data.ReadInt32();
296 sptr <IRemoteObject> callback = data.ReadRemoteObject();
297 int32_t result = MechBodyControllerService::GetInstance().StopMoving(mechId, cmdId);
298 bool writeResult = reply.WriteInt32(result);
299 HILOGI("%{public}d", writeResult);
300 return result;
301 }
302
GetRotationAnglesInner(MessageParcel & data,MessageParcel & reply)303 int32_t MechBodyControllerStub::GetRotationAnglesInner(MessageParcel &data, MessageParcel &reply)
304 {
305 int32_t mechId = data.ReadInt32();
306 std::shared_ptr<EulerAngles> currentPosition = std::make_shared<EulerAngles>();
307 int32_t result = MechBodyControllerService::GetInstance().GetRotationAngles(mechId, currentPosition);
308 bool writeResult = reply.WriteInt32(result);
309 if (result == ERR_OK) {
310 HILOGI("current position: %{public}s", currentPosition->ToString().c_str());
311 writeResult = reply.WriteParcelable(currentPosition.get());
312 }
313 HILOGI("%{public}d", writeResult);
314 return result;
315 }
316
GetRotationDegreeLimitsInner(MessageParcel & data,MessageParcel & reply)317 int32_t MechBodyControllerStub::GetRotationDegreeLimitsInner(MessageParcel &data, MessageParcel &reply)
318 {
319 int32_t mechId = data.ReadInt32();
320 RotateDegreeLimit rotationLimit;
321 int32_t result = MechBodyControllerService::GetInstance().GetRotationDegreeLimits(mechId, rotationLimit);
322 bool writeResult = reply.WriteInt32(result);
323 if (result == ERR_OK) {
324 HILOGI("RotateDegreeLimit: %{public}s", rotationLimit.ToString().c_str());
325 writeResult = reply.WriteParcelable(&rotationLimit);
326 }
327 HILOGI("%{public}d", writeResult);
328 return result;
329 }
330
GetRotationAxesStatusInner(MessageParcel & data,MessageParcel & reply)331 int32_t MechBodyControllerStub::GetRotationAxesStatusInner(MessageParcel &data, MessageParcel &reply)
332 {
333 int32_t mechId = data.ReadInt32();
334 RotationAxesStatus axesStatus;
335 int32_t result = MechBodyControllerService::GetInstance().GetRotationAxesStatus(mechId, axesStatus);
336 bool writeResult = reply.WriteInt32(result);
337 if (result == ERR_OK) {
338 HILOGI("RotationAxesStatus: %{public}s", axesStatus.ToString().c_str());
339 writeResult = reply.WriteParcelable(&axesStatus);
340 }
341 HILOGI("%{public}d", writeResult);
342 return result;
343 }
344
RotationAxesStatusChangeListenOnInner(MessageParcel & data,MessageParcel & reply)345 int32_t MechBodyControllerStub::RotationAxesStatusChangeListenOnInner(MessageParcel &data, MessageParcel &reply)
346 {
347 sptr <IRemoteObject> callback = data.ReadRemoteObject();
348
349 int32_t result = MechBodyControllerService::GetInstance().RegisterRotationAxesStatusChangeCallback(callback);
350 bool writeResult = reply.WriteInt32(result);
351 HILOGI("%{public}d", writeResult);
352 return result;
353 }
354
RotationAxesStatusChangeListenOffInner(MessageParcel & data,MessageParcel & reply)355 int32_t MechBodyControllerStub::RotationAxesStatusChangeListenOffInner(MessageParcel &data, MessageParcel &reply)
356 {
357 int32_t result = MechBodyControllerService::GetInstance().UnRegisterRotationAxesStatusChangeCallback();
358 bool writeResult = reply.WriteInt32(result);
359 HILOGI("%{public}d", writeResult);
360 return result;
361 }
362 } // namespace MechBodyController
363 } // namespace OHOS
364