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_config_cmd.h"
17
18 #include "mechbody_controller_log.h"
19
20 namespace OHOS {
21 namespace MechBodyController {
22 namespace {
23 const std::string TAG = "SetMechConfigCmd";
24 }
25
SetMechConfigCmd(uint8_t configVersion)26 SetMechConfigCmd::SetMechConfigCmd(uint8_t configVersion)
27 {
28 cmdSet_ = CMD_SET;
29 cmdId_ = CMD_ID;
30 reqSize_ = REQ_SIZE;
31 rspSize_ = RSP_SIZE;
32 needResponse_ = (RSP_SIZE > 0);
33 timeoutMs_ = MECHBODY_MSG_TIMEOUT;
34 configVersion_ = configVersion;
35 retryTimes_ = CMD_PRIORITY_MIDDLE;
36 }
37
Marshal() const38 std::shared_ptr<MechDataBuffer> SetMechConfigCmd::Marshal() const
39 {
40 HILOGI("start.");
41 auto buffer = std::make_shared<MechDataBuffer>(reqSize_ + BIT_OFFSET_2);
42 if (buffer == nullptr) {
43 HILOGE("Failed to allocate memory for Marshal buffer");
44 return nullptr;
45 }
46
47 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdSet_), nullptr, "append cmdSet_");
48 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdId_), nullptr, "append cmdId_");
49 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(MC_CONFIG_VERSION), nullptr, "append configVersion_");
50
51 HILOGI("end.");
52 return buffer;
53 }
54
TriggerResponse(std::shared_ptr<MechDataBuffer> data)55 void SetMechConfigCmd::TriggerResponse(std::shared_ptr<MechDataBuffer> data)
56 {
57 if (data == nullptr || data->Size() < RSP_SIZE + BIT_OFFSET_2) {
58 HILOGE("Invalid input data for response");
59 return;
60 }
61
62 if (data->ReadUint8(BIT_OFFSET_2, result_) != ERR_OK) {
63 HILOGE("read result failed");
64 return;
65 }
66 HILOGI("response code: %{public}u", result_);
67
68 if (responseCb_) {
69 HILOGI("trigger response callback.");
70 responseCb_();
71 }
72 }
73
GetResult() const74 uint8_t SetMechConfigCmd::GetResult() const
75 {
76 return result_;
77 }
78 } // namespace MechBodyController
79 } // namespace OHOS
80