• 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_get_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 = "GetMechCameraTrackingLayoutCmd";
24     constexpr uint8_t CMD_GET_TRACKING_LAYOUT_TYPE = 0x02;
25     constexpr uint8_t CMD_GET_TRACKING_LAYOUT_REPLY_LENGTH = 9;
26 }
27 
GetMechCameraTrackingLayoutCmd()28 GetMechCameraTrackingLayoutCmd::GetMechCameraTrackingLayoutCmd()
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> GetMechCameraTrackingLayoutCmd::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_TRACKING_LAYOUT_TYPE), nullptr, "append get layout type");
51     CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(BIT_1), nullptr, "append get layout len");
52     CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(BIT_0), nullptr, "append get layout value");
53 
54     HILOGI("end.");
55     return buffer;
56 }
57 
TriggerResponse(std::shared_ptr<MechDataBuffer> data)58 void GetMechCameraTrackingLayoutCmd::TriggerResponse(std::shared_ptr<MechDataBuffer> data)
59 {
60     HILOGI("start.");
61     if (data == nullptr || data->Size() < RSP_SIZE + BIT_OFFSET_2) {
62         HILOGE("Invalid input data for response");
63         return;
64     }
65 
66     size_t offset = BIT_OFFSET_2;
67 
68     CHECK_ERR_RETURN(data->ReadUint8(offset, result_), "read result_");
69     HILOGI("response code: %{public}u", result_);
70     offset++;
71 
72     uint8_t resultType = 0;
73     CHECK_ERR_RETURN(data->ReadUint8(offset, resultType), "read resultType");
74     if (resultType != CMD_GET_TRACKING_LAYOUT_TYPE) {
75         HILOGE("Reply data resultType invalid");
76         return;
77     }
78     offset++;
79 
80     uint8_t resultLength = 0;
81     CHECK_ERR_RETURN(data->ReadUint8(offset, resultLength), "read resultLength");
82     if (resultLength != CMD_GET_TRACKING_LAYOUT_REPLY_LENGTH) {
83         HILOGE("Reply data resultLength invalid");
84         return;
85     }
86     offset++;
87 
88     uint8_t controlBit = 0;
89     CHECK_ERR_RETURN(data->ReadUint8(offset, controlBit), "read controlBit");
90     params_.isDefault = (controlBit & BIT_1);
91     HILOGI("isDefault : %{public}d", params_.isDefault);
92     offset++;
93 
94     float offsetX = 0.0f;
95     float offsetY = 0.0f;
96     CHECK_ERR_RETURN(data->ReadFloat(offset, offsetX), "read offsetX");
97     offset += sizeof(float);
98 
99     CHECK_ERR_RETURN(data->ReadFloat(offset, offsetY), "read offsetY");
100 
101     params_.offsetX = offsetX;
102     params_.offsetY = offsetY;
103     HILOGI("offsetX: %{public}f, offsetY: %{public}f", params_.offsetX, params_.offsetY);
104 
105     if (responseCb_) {
106         HILOGI("trigger response callback.");
107         responseCb_();
108     }
109     HILOGI("end.");
110 }
111 
GetParams() const112 const LayoutParams& GetMechCameraTrackingLayoutCmd::GetParams() const
113 {
114     return params_;
115 }
116 
GetResult() const117 uint8_t GetMechCameraTrackingLayoutCmd::GetResult() const
118 {
119     return result_;
120 }
121 } // namespace MechBodyController
122 } // namespace OHOS
123