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_set_mech_stop_cmd.h"
17
18 #include "mechbody_controller_log.h"
19
20 namespace OHOS {
21 namespace MechBodyController {
22 namespace {
23 const std::string TAG = "SetMechStopCmd";
24 constexpr uint8_t CMD_MECH_STOP_TYPE = 0x04;
25 constexpr uint8_t CMD_MECH_STOP_SIZE = 5;
26 constexpr uint8_t CMD_MECH_STOP_CONTROL = 0b11000000;
27 }
28
SetMechStopCmd()29 SetMechStopCmd::SetMechStopCmd()
30 {
31 cmdSet_ = CMD_SET;
32 cmdId_ = CMD_ID;
33 reqSize_ = REQ_SIZE;
34 rspSize_ = RSP_SIZE;
35 needResponse_ = (RSP_SIZE > 0);
36 timeoutMs_ = MECHBODY_MSG_TIMEOUT;
37 retryTimes_ = CMD_PRIORITY_MIDDLE;
38 }
39
Marshal() const40 std::shared_ptr<MechDataBuffer> SetMechStopCmd::Marshal() const
41 {
42 HILOGI("start.");
43 auto buffer = std::make_shared<MechDataBuffer>(reqSize_ + BIT_OFFSET_2);
44 if (buffer == nullptr) {
45 HILOGE("Failed to allocate memory for Marshal buffer");
46 return nullptr;
47 }
48
49 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdSet_), nullptr, "append cmdSet_");
50 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdId_), nullptr, "append cmdId_");
51
52 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(CMD_MECH_STOP_TYPE), nullptr, "append tlv type");
53 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(CMD_MECH_STOP_SIZE), nullptr, "append tlv size");
54
55 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(CMD_MECH_STOP_CONTROL), nullptr, "append control byte");
56 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(BIT_0), nullptr, "append reserve byte");
57 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(BIT_0), nullptr, "append reserve byte");
58 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(BIT_0), nullptr, "append point number");
59 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(BIT_0), nullptr, "append reserve byte");
60
61 HILOGI("end.");
62 return buffer;
63 }
64
TriggerResponse(std::shared_ptr<MechDataBuffer> data)65 void SetMechStopCmd::TriggerResponse(std::shared_ptr<MechDataBuffer> data)
66 {
67 if (data == nullptr || data->Size() < RSP_SIZE + BIT_OFFSET_2) {
68 HILOGE("Invalid input data for response");
69 return;
70 }
71
72 CHECK_ERR_RETURN(data->ReadUint8(BIT_OFFSET_2, result_), "read result_");
73 HILOGI("response code: %{public}u", result_);
74
75 if (responseCb_) {
76 HILOGI("trigger response callback.");
77 responseCb_();
78 }
79 }
80
GetResult() const81 uint8_t SetMechStopCmd::GetResult() const
82 {
83 return result_;
84 }
85 } // namespace MechBodyController
86 } // namespace OHOS
87