• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_register_mech_control_result_cmd.h"
17 
18 #include "mechbody_controller_log.h"
19 
20 namespace OHOS {
21 namespace MechBodyController {
22 namespace {
23     const std::string TAG = "RegisterMechControlResultCmd";
24 }
25 
RegisterMechControlResultCmd()26 RegisterMechControlResultCmd::RegisterMechControlResultCmd()
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     retryTimes_ = CMD_PRIORITY_MIDDLE;
35 }
36 
Marshal() const37 std::shared_ptr<MechDataBuffer> RegisterMechControlResultCmd::Marshal() const
38 {
39     HILOGI("start.");
40     auto buffer = std::make_shared<MechDataBuffer>(reqSize_ + BIT_OFFSET_2);
41     if (buffer == nullptr) {
42         HILOGE("Failed to allocate memory for Marshal buffer");
43         return nullptr;
44     }
45 
46     CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdSet_), nullptr, "append cmdSet_");
47     CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdId_), nullptr, "append cmdId_");
48 
49     HILOGI("end.");
50     return buffer;
51 }
52 
Unmarshal(std::shared_ptr<MechDataBuffer> data)53 bool RegisterMechControlResultCmd::Unmarshal(std::shared_ptr<MechDataBuffer> data)
54 {
55     HILOGI("start.");
56     if (data == nullptr || data->Size() < RPT_SIZE + BIT_OFFSET_2) {
57         HILOGE("Invalid input data for unmarshal");
58         return false;
59     }
60 
61     CHECK_ERR_RETURN_VALUE(data->ReadUint8(BIT_OFFSET_2, controlResult_), false, "read controlResult_");
62     CHECK_ERR_RETURN_VALUE(data->ReadUint8(BIT_OFFSET_3, taskId_), false, "read taskId_");
63     HILOGI("get controlResult_: %{public}u. taskId_: %{public}u", controlResult_, taskId_);
64 
65     return true;
66 }
67 
TriggerResponse(std::shared_ptr<MechDataBuffer> data)68 void RegisterMechControlResultCmd::TriggerResponse(std::shared_ptr<MechDataBuffer> data)
69 {
70     HILOGI("start.");
71     if (responseCb_) {
72         HILOGI("trigger response callback.");
73         responseCb_();
74     }
75     HILOGI("end.");
76 }
77 
GetControlResult() const78 uint8_t RegisterMechControlResultCmd::GetControlResult() const
79 {
80     return controlResult_;
81 }
82 
GetResult() const83 uint8_t RegisterMechControlResultCmd::GetResult() const
84 {
85     return result_;
86 }
87 
GetTaskId() const88 uint8_t RegisterMechControlResultCmd::GetTaskId() const
89 {
90     return taskId_;
91 }
92 } // namespace MechBodyController
93 } // namespace OHOS
94