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 "mc_get_mech_capability_info_cmd.h"
17
18 #include "mechbody_controller_log.h"
19
20 namespace OHOS {
21 namespace MechBodyController {
22 namespace {
23 const std::string TAG = "GetMechCapabilityInfoCmd";
24 static constexpr uint8_t CMD_GET_MECH_LIMIT_TYPE = 0x03;
25 constexpr uint8_t CMD_GET_MECH_LIMIT_REPLY_LENGTH = 24;
26 }
27
GetMechCapabilityInfoCmd()28 GetMechCapabilityInfoCmd::GetMechCapabilityInfoCmd()
29 {
30 cmdSet_ = CMD_SET;
31 cmdId_ = CMD_ID;
32 reqSize_ = REQ_SIZE;
33 rspSize_ = RSP_SIZE;
34 needResponse_ = (RSP_SIZE > 0);
35 timeoutMs_ = MECHBODY_MSG_TIMEOUT;
36 retryTimes_ = CMD_PRIORITY_MIDDLE;
37 }
38
Marshal() const39 std::shared_ptr<MechDataBuffer> GetMechCapabilityInfoCmd::Marshal() const
40 {
41 HILOGI("start.");
42 auto buffer = std::make_shared<MechDataBuffer>(reqSize_ + BIT_OFFSET_2);
43 if (buffer == nullptr) {
44 HILOGE("Failed to allocate memory for Marshal buffer");
45 return nullptr;
46 }
47
48 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdSet_), nullptr, "append cmdSet_");
49 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdId_), nullptr, "append cmdId_");
50 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(CMD_GET_MECH_LIMIT_TYPE), nullptr, "append get mech limit type");
51 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(BIT_0), nullptr, "append get mech limit len");
52 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(BIT_0), nullptr, "append get mech limit value");
53
54 HILOGI("end.");
55 return buffer;
56 }
57
TriggerResponse(std::shared_ptr<MechDataBuffer> data)58 void GetMechCapabilityInfoCmd::TriggerResponse(std::shared_ptr<MechDataBuffer> data)
59 {
60 if (data == nullptr || data->Size() < RSP_SIZE + BIT_OFFSET_2) {
61 HILOGE("Invalid input data for response");
62 return;
63 }
64
65 size_t offset = BIT_OFFSET_2;
66
67 CHECK_ERR_RETURN(data->ReadUint8(offset, result_), "read result_");
68 HILOGI("response code: %{public}u", result_);
69 offset++;
70
71 uint8_t resultType = 0;
72 CHECK_ERR_RETURN(data->ReadUint8(offset, resultType), "read resultType");
73 if (resultType != CMD_GET_MECH_LIMIT_TYPE) {
74 HILOGE("Reply data resultType invalid");
75 return;
76 }
77 offset++;
78
79 uint8_t resultLength = 0;
80 CHECK_ERR_RETURN(data->ReadUint8(offset, resultLength), "read resultLength");
81 if (resultLength != CMD_GET_MECH_LIMIT_REPLY_LENGTH) {
82 HILOGE("Reply data resultLength invalid");
83 return;
84 }
85 offset++;
86
87 CHECK_ERR_RETURN(data->ReadFloat(offset, params_.posMax.roll), "read rollMax");
88 offset += sizeof(float);
89
90 CHECK_ERR_RETURN(data->ReadFloat(offset, params_.negMax.roll), "read rollMin");
91 offset += sizeof(float);
92
93 CHECK_ERR_RETURN(data->ReadFloat(offset, params_.posMax.pitch), "read pitchMax");
94 offset += sizeof(float);
95
96 CHECK_ERR_RETURN(data->ReadFloat(offset, params_.negMax.pitch), "read pitchMin");
97 offset += sizeof(float);
98
99 CHECK_ERR_RETURN(data->ReadFloat(offset, params_.posMax.yaw), "read yawMax");
100 offset += sizeof(float);
101
102 CHECK_ERR_RETURN(data->ReadFloat(offset, params_.negMax.yaw), "read yawMin");
103 offset += sizeof(float);
104
105 if (responseCb_) {
106 HILOGI("trigger response callback.");
107 responseCb_();
108 }
109 }
110
GetParams() const111 const RotateDegreeLimit& GetMechCapabilityInfoCmd::GetParams() const
112 {
113 return params_;
114 }
115
GetResult() const116 uint8_t GetMechCapabilityInfoCmd::GetResult() const
117 {
118 return result_;
119 }
120 } // namespace MechBodyController
121 } // namespace OHOS
122