• 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_set_mech_camera_tracking_layout_cmd.h"
17 
18 #include "mechbody_controller_log.h"
19 
20 namespace OHOS {
21 namespace MechBodyController {
22 namespace {
23     const std::string TAG = "SetMechCameraTrackingLayoutCmd";
24     constexpr uint8_t CMD_SET_TRACKING_LAYOUT_TYPE = 0x02;
25     constexpr uint8_t CMD_SET_TRACKING_LAYOUT_LENGTH = 9;
26     constexpr uint8_t CMD_SET_LAYOUT_DEFAULT = 0b00000000;
27     constexpr uint8_t CMD_SET_LAYOUT_NOT_DEFAULT = 0b00000001;
28 }
29 
SetMechCameraTrackingLayoutCmd(const LayoutParams & params)30 SetMechCameraTrackingLayoutCmd::SetMechCameraTrackingLayoutCmd(const LayoutParams& params)
31     : params_(params)
32 {
33     cmdSet_ = CMD_SET;
34     cmdId_ = CMD_ID;
35     reqSize_ = REQ_SIZE;
36     rspSize_ = RSP_SIZE;
37     needResponse_ = (RSP_SIZE > 0);
38     timeoutMs_ = MECHBODY_MSG_TIMEOUT;
39     retryTimes_ = CMD_PRIORITY_MIDDLE;
40 }
41 
Marshal() const42 std::shared_ptr<MechDataBuffer> SetMechCameraTrackingLayoutCmd::Marshal() const
43 {
44     HILOGD("start.");
45     auto buffer = std::make_shared<MechDataBuffer>(reqSize_ + BIT_OFFSET_2);
46     if (buffer == nullptr) {
47         HILOGE("Failed to allocate memory for Marshal buffer");
48         return nullptr;
49     }
50 
51     CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdSet_), nullptr, "append cmdSet_");
52     CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdId_), nullptr, "append cmdId_");
53 
54     CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(CMD_SET_TRACKING_LAYOUT_TYPE), nullptr, "append control type");
55     CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(CMD_SET_TRACKING_LAYOUT_LENGTH), nullptr, "append control len");
56     if (params_.isDefault) {
57         CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(CMD_SET_LAYOUT_DEFAULT), nullptr, "append control byte");
58     } else {
59         CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(CMD_SET_LAYOUT_NOT_DEFAULT), nullptr, "append control byte");
60         CHECK_ERR_RETURN_VALUE(
61             buffer->AppendFloat(params_.offsetX), nullptr, "append offsetX");
62         CHECK_ERR_RETURN_VALUE(
63             buffer->AppendFloat(params_.offsetY), nullptr, "append offsetY");
64     }
65 
66     HILOGD("end.");
67     return buffer;
68 }
69 
TriggerResponse(std::shared_ptr<MechDataBuffer> data)70 void SetMechCameraTrackingLayoutCmd::TriggerResponse(std::shared_ptr<MechDataBuffer> data)
71 {
72     if (data == nullptr || data->Size() < RSP_SIZE + BIT_OFFSET_2) {
73         HILOGE("Invalid input data for response");
74         return;
75     }
76 
77     CHECK_ERR_RETURN(data->ReadUint8(BIT_OFFSET_2, result_), "read result_");
78     HILOGI("response code: %{public}u", result_);
79 
80     if (responseCb_) {
81         HILOGI("trigger response callback.");
82         responseCb_();
83     }
84 }
85 
GetParams() const86 const LayoutParams& SetMechCameraTrackingLayoutCmd::GetParams() const
87 {
88     return params_;
89 }
90 
GetResult() const91 uint8_t SetMechCameraTrackingLayoutCmd::GetResult() const
92 {
93     return result_;
94 }
95 } // namespace MechBodyController
96 } // namespace OHOS
97